expressots-cron
Version:
Expressots - Cron Provider (expressots-cron)
35 lines (34 loc) • 1.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cron = Cron;
const core_1 = require("@expressots/core");
const utils_1 = require("../utils");
const cron_metadata_1 = require("../utils/cron.metadata");
const cron_db_1 = __importDefault(require("../db/cron.db"));
const constants_1 = require("../constants");
/* eslint-disable @typescript-eslint/no-explicit-any */
function Cron(cronExpression, options) {
const report = new core_1.Report();
return function (target, propertyKey, descriptor) {
(0, cron_metadata_1.setCronMetadata)(target, propertyKey);
const originalMethod = descriptor.value;
try {
descriptor.value = function (...args) {
const result = (0, utils_1.createCron)(cronExpression, () => originalMethod.apply(this, args), {
...options,
startImmediately: true,
});
cron_db_1.default.addJob(options?.name, result);
return result;
};
return descriptor;
}
catch (error) {
console.log(error);
report.error("Unexpected error on cron decorator", 500, constants_1.EXPRESSOTS_CRON);
}
};
}