@grouparoo/core
Version:
The Grouparoo Core
54 lines (53 loc) • 1.92 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateRunCounts = void 0;
const Run_1 = require("../../models/Run");
const sequelize_1 = require("sequelize");
const moment_1 = __importDefault(require("moment"));
const clsTask_1 = require("../../classes/tasks/clsTask");
class UpdateRunCounts extends clsTask_1.CLSTask {
constructor() {
super(...arguments);
this.name = "runs:updateCounts";
this.description = "Update the counts of imports and records for runs";
this.frequency = 1000 * 15;
this.queue = "runs";
}
async runWithinTransaction() {
const since = (0, moment_1.default)().subtract(1, "day").toDate();
const runs = await Run_1.Run.findAll({
where: {
state: { [sequelize_1.Op.ne]: "stopped" },
error: { [sequelize_1.Op.eq]: null },
updatedAt: { [sequelize_1.Op.gte]: since },
[sequelize_1.Op.or]: [
{ importsCreated: 0 },
{
recordsImported: {
[sequelize_1.Op.lt]: sequelize_1.Sequelize.col("importsCreated"),
},
},
],
},
});
const errors = [];
for (const run of runs) {
try {
await run.updateTotals();
}
catch (_error) {
errors.push(_error);
}
}
if (errors.length > 0) {
const error = new Error(`Error updating runs: ${errors.join(", ")}`);
error["errors"] = errors;
throw error;
}
return runs.length;
}
}
exports.UpdateRunCounts = UpdateRunCounts;