UNPKG

@grouparoo/core

Version:
40 lines (33 loc) 1.22 kB
import { Setting } from "../../models/Setting"; import { Run } from "../../models/Run"; import { internalRun } from "../../modules/internalRun"; import { CLSTask } from "../../classes/tasks/clsTask"; import { getGrouparooRunMode } from "../../modules/runMode"; export class RunRecurringInternalRun extends CLSTask { name = "run:recurringInternalRun"; description = "check if we should run an internal import on a frequency"; frequency = getGrouparooRunMode() === "cli:run" ? 0 : 1000 * 60 * 10; // 10 minutes queue = "runs"; async runWithinTransaction() { const setting = await 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.findOne({ where: { creatorType: "task" }, order: [["createdAt", "desc"]], }); let toRun = false; if (!lastRun) { toRun = true; } if (new Date().getTime() - lastRun?.createdAt?.getTime() >= frequencyInMs) { toRun = true; } if (lastRun?.state === "running") { toRun = false; } if (toRun) await internalRun("task", this.name); } }