UNPKG

@grouparoo/core

Version:
62 lines (61 loc) 2.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GrouparooRecordsConfirm = void 0; const moment_1 = __importDefault(require("moment")); const actionhero_1 = require("actionhero"); const clsTask_1 = require("../../classes/tasks/clsTask"); const cls_1 = require("../../modules/cls"); const plugin_1 = require("../../modules/plugin"); const Schedule_1 = require("../../models/Schedule"); const Run_1 = require("../../models/Run"); const record_1 = require("../../modules/ops/record"); class GrouparooRecordsConfirm extends clsTask_1.CLSTask { constructor() { super(...arguments); this.name = "records:confirm"; this.description = "Confirm that records still exist in the source"; this.frequency = 1000 * 30; this.queue = "records"; this.inputs = {}; } async runWithinTransaction() { const limit = actionhero_1.config.batchSize.imports; const confirmDays = parseInt((await plugin_1.plugin.readSetting("core", "confirm-records-days")).value); let count = 0; const schedules = await Schedule_1.Schedule.findAll({ where: { confirmRecords: true }, include: [ { model: Run_1.Run, limit: 1, where: { state: "complete" }, order: [["completedAt", "desc"]], required: true, }, ], }); for (const schedule of schedules) { const latestRun = schedule.runs[0]; if (!latestRun) continue; const pendingImports = await latestRun.$count("imports", { where: { state: "associating" }, }); if (pendingImports !== 0) continue; count += await record_1.RecordOps.confirmExistence(limit - count, latestRun.completedAt, schedule.sourceId); } if (confirmDays > 0) { const nextConfirmAt = (0, moment_1.default)().subtract(confirmDays, "days").toDate(); count += await record_1.RecordOps.confirmExistence(limit - count, nextConfirmAt); } // re-enqueue if there is more to do if (count > 0) await cls_1.CLS.enqueueTask(this.name, {}); return count; } } exports.GrouparooRecordsConfirm = GrouparooRecordsConfirm;