@grouparoo/core
Version:
The Grouparoo Core
44 lines (43 loc) • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RunRecurringInternalRun = void 0;
const Setting_1 = require("../../models/Setting");
const Run_1 = require("../../models/Run");
const internalRun_1 = require("../../modules/internalRun");
const clsTask_1 = require("../../classes/tasks/clsTask");
const runMode_1 = require("../../modules/runMode");
class RunRecurringInternalRun extends clsTask_1.CLSTask {
constructor() {
super(...arguments);
this.name = "run:recurringInternalRun";
this.description = "check if we should run an internal import on a frequency";
this.frequency = (0, runMode_1.getGrouparooRunMode)() === "cli:run" ? 0 : 1000 * 60 * 10; // 10 minutes
this.queue = "runs";
}
async runWithinTransaction() {
var _a;
const setting = await Setting_1.Setting.findOne({
where: { key: "runs-recurring-internal-run-frequency-hours" },
});
const frequencyInMs = parseInt(setting.value) * 60 * 60 * 1000;
if (frequencyInMs <= 0)
return;
const lastRun = await Run_1.Run.findOne({
where: { creatorType: "task" },
order: [["createdAt", "desc"]],
});
let toRun = false;
if (!lastRun) {
toRun = true;
}
if (new Date().getTime() - ((_a = lastRun === null || lastRun === void 0 ? void 0 : lastRun.createdAt) === null || _a === void 0 ? void 0 : _a.getTime()) >= frequencyInMs) {
toRun = true;
}
if ((lastRun === null || lastRun === void 0 ? void 0 : lastRun.state) === "running") {
toRun = false;
}
if (toRun)
await (0, internalRun_1.internalRun)("task", this.name);
}
}
exports.RunRecurringInternalRun = RunRecurringInternalRun;