@grouparoo/core
Version:
The Grouparoo Core
145 lines (144 loc) • 5.75 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunCLI = void 0;
const actionhero_1 = require("actionhero");
const cli_1 = require("../modules/cli");
const reset_1 = require("../modules/reset");
const runMode_1 = require("../modules/runMode");
const Schedule_1 = require("../models/Schedule");
const Run_1 = require("../models/Run");
class RunCLI extends actionhero_1.CLI {
constructor() {
super();
this.name = "run";
this.description = "Run all Schedules, Runs, Imports and Exports pending in this cluster. Use GROUPAROO_LOG_LEVEL env to set log level.";
this.inputs = {
reset: {
default: false,
description: "[DANGER] Empty the cluster of all GrouparooRecord data before starting the run? Equivalent to `grouparoo reset data`",
letter: "r",
flag: true,
},
"reset-high-watermarks": {
default: false,
description: "Should we run all Schedules from the beginning?",
letter: "m",
flag: true,
},
"no-export": {
description: "Skip exporting the records",
letter: "n",
flag: true,
},
web: {
default: false,
description: "Enable the web server during this run?",
letter: "w",
flag: true,
},
scheduleIds: {
description: "Only run specific Schedules by id",
required: false,
requiredValue: true,
variadic: true,
letter: "s",
placeholder: "schedule ids",
},
};
this.preInitialize = () => {
cli_1.GrouparooCLI.setGrouparooRunMode(this);
cli_1.GrouparooCLI.setNextDevelopmentMode();
};
cli_1.GrouparooCLI.timestampOption(this);
}
async run({ params, }) {
var _a, _b;
cli_1.GrouparooCLI.logCLI(this.name, false);
this.checkWorkers();
if (!params.web)
cli_1.GrouparooCLI.disableWebServer();
if (params.reset)
await reset_1.Reset.data((0, runMode_1.getGrouparooRunMode)());
if (params.resetHighWatermarks)
await reset_1.Reset.resetHighWatermarks();
process.env.GROUPAROO_DISABLE_EXPORTS = String(((_b = (_a = params.export) === null || _a === void 0 ? void 0 : _a.toString()) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== "true");
const { main } = await Promise.resolve().then(() => __importStar(require("../grouparoo")));
await main();
await this.checkSchedules(params.scheduleIds);
await this.stopScheduleRuns();
const scheduleIds = Array.isArray(params.scheduleIds)
? params.scheduleIds
: undefined;
await this.runTasks(scheduleIds);
return false;
}
checkWorkers() {
if (actionhero_1.config.tasks.minTaskProcessors < 1) {
return cli_1.GrouparooCLI.logger.fatal(`No Task Workers are enabled. Modify your environment to add Workers`);
}
}
async checkSchedules(scheduleIds) {
if (typeof scheduleIds === "undefined")
return;
const schedules = await Schedule_1.Schedule.findAll({ where: { id: scheduleIds } });
const foundScheduleIds = schedules.map((s) => s.id);
scheduleIds.forEach((id) => {
if (!foundScheduleIds.includes(id))
return cli_1.GrouparooCLI.logger.fatal(`Schedule with id "${id}" was not found`);
});
}
async stopScheduleRuns() {
const runningRuns = await Run_1.Run.findAll({
where: {
state: "running",
creatorType: "schedule",
},
});
for (const run of runningRuns) {
await run.stop();
}
if (runningRuns.length) {
(0, actionhero_1.log)(`Stopped ${runningRuns.length} previously running Schedules`, "notice");
}
}
async runTasks(scheduleIds) {
const tasks = {
"appRefreshQueries:check": {},
"schedules:enqueueRuns": {
ignoreDeltas: true,
runIfNotRecurring: true,
scheduleIds,
},
"run:recurringInternalRun": {},
"group:updateCalculatedGroups": {},
};
for (const [name, args] of Object.entries(tasks)) {
const task = actionhero_1.api.tasks.tasks[name];
await task.run(args, {});
}
}
}
exports.RunCLI = RunCLI;