toad-scheduler
Version:
In-memory Node.js and browser job scheduler
56 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CronJob = exports.CRON_EVERY_HOUR = exports.CRON_EVERY_30_MINUTES = exports.CRON_EVERY_MINUTE = exports.CRON_EVERY_30_SECONDS = exports.CRON_EVERY_SECOND = void 0;
const Job_1 = require("../../common/Job");
const croner_1 = require("croner");
exports.CRON_EVERY_SECOND = '* * * * * *';
exports.CRON_EVERY_30_SECONDS = '*/30 * * * * *';
exports.CRON_EVERY_MINUTE = '* * * * *';
exports.CRON_EVERY_30_MINUTES = '*/30 * * * *';
exports.CRON_EVERY_HOUR = '0 * * * *';
class CronJob extends Job_1.Job {
constructor(schedule, task, options = {}) {
super(options.id);
this.preventOverrun = options.preventOverrun || false;
this.unref = options.unref;
this.schedule = schedule;
this.task = task;
}
/* istanbul ignore next */
getStatus() {
var _a;
return ((_a = this.cronInstance) === null || _a === void 0 ? void 0 : _a.isRunning()) ? Job_1.JobStatus.RUNNING : Job_1.JobStatus.STOPPED;
}
start() {
this.cronInstance = new croner_1.Cron(this.schedule.cronExpression, {
timezone: this.schedule.timezone,
protect: false,
unref: this.unref,
}, () => {
if (!this.task.isExecuting || !this.preventOverrun) {
this.task.execute(this.id);
}
});
}
/* istanbul ignore next */
stop() {
var _a;
(_a = this.cronInstance) === null || _a === void 0 ? void 0 : _a.stop();
}
applyUnrefDefault(unref) {
if (this.unref !== undefined) {
return;
}
this.unref = unref;
// The scheduler applies defaults before starting the job, but a job that
// was started manually beforehand has already handed the old value to
// croner — recreate the instance so the resolved value takes effect,
// mirroring how the interval jobs unref an already-running timer.
if (this.unref && this.cronInstance) {
this.stop();
this.start();
}
}
}
exports.CronJob = CronJob;
//# sourceMappingURL=CronJob.js.map