UNPKG

@grouparoo/core

Version:
51 lines (50 loc) 2.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroupsUpdateCalculatedGroups = void 0; const Group_1 = require("../../models/Group"); const Run_1 = require("../../models/Run"); const plugin_1 = require("../../modules/plugin"); const moment_1 = __importDefault(require("moment")); const clsTask_1 = require("../../classes/tasks/clsTask"); const runMode_1 = require("../../modules/runMode"); class GroupsUpdateCalculatedGroups extends clsTask_1.CLSTask { constructor() { super(...arguments); this.name = "group:updateCalculatedGroups"; this.description = "enqueue an update of groups that to be updated"; this.frequency = (0, runMode_1.getGrouparooRunMode)() === "cli:run" ? 0 : 1000 * 60 * 5; // Run every 5 minutes this.queue = "groups"; } async runWithinTransaction() { var _a, _b; const setting = await plugin_1.plugin.readSetting("core", "groups-calculation-delay-minutes"); const delayMinutes = parseInt(setting.value); const lastCheckTime = (0, moment_1.default)().subtract(delayMinutes, "minutes").toDate(); const groupsToRun = []; const calculatedGroups = await Group_1.Group.scope(null).findAll(); for (const group of calculatedGroups) { const calculatedAt = (_b = (_a = group.calculatedAt) === null || _a === void 0 ? void 0 : _a.getTime()) !== null && _b !== void 0 ? _b : 0; const nextCalculatedAt = await group.nextCalculatedAt(); if (nextCalculatedAt && calculatedAt < lastCheckTime.getTime()) { if (group.state === "ready") { groupsToRun.push(group); } else if (group.state === "updating") { const runningRun = await Run_1.Run.findOne({ where: { creatorId: group.id, state: "running" }, }); // the group is stuck in "updating" and has no run working it if (!runningRun) groupsToRun.push(group); } } } for (const group of groupsToRun) { await group.run(); } } } exports.GroupsUpdateCalculatedGroups = GroupsUpdateCalculatedGroups;