@grouparoo/core
Version:
The Grouparoo Core
52 lines (51 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunsTick = void 0;
const Run_1 = require("../../models/Run");
const clsTask_1 = require("../../classes/tasks/clsTask");
const actionhero_1 = require("actionhero");
const cls_1 = require("../../modules/cls");
const sequelize_1 = require("sequelize");
const runs_1 = require("../../modules/ops/runs");
class RunsTick extends clsTask_1.CLSTask {
constructor() {
super(...arguments);
this.name = "run:tick";
this.description = "process pending runs and enqueue the next batch";
this.frequency = actionhero_1.config.tasks.timeout + 1;
this.queue = "runs";
}
async runWithinTransaction() {
const lastCheck = new Date().getTime() - Math.max(this.frequency, 1000);
const runs = await Run_1.Run.findAll({
where: { state: "running", updatedAt: { [sequelize_1.Op.lt]: lastCheck } },
});
let runsEnqueued = 0;
for (const run of runs) {
const args = { runId: run.id };
let taskName = "";
if (run.method === "internalRun") {
taskName = "run:internalRun";
}
else if (run.creatorType === "group") {
taskName = "group:run";
}
else if (run.creatorType === "schedule") {
taskName = "schedule:run";
}
else if (run.creatorType === "grouparooModel") {
taskName = "grouparooModel:run";
}
else {
throw new Error(`cannot tick run ${run.id}`);
}
const enqueued = await runs_1.RunOps.isRunEnqueued(taskName, run.id);
if (!enqueued) {
cls_1.CLS.enqueueTask(taskName, args);
runsEnqueued++;
}
}
return runsEnqueued;
}
}
exports.RunsTick = RunsTick;