@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
47 lines • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Task = void 0;
const matcher_1 = require("./matcher");
const container_1 = require("../../container");
const corn_parser_1 = require("./corn-parser");
const helpers_1 = require("../../helpers");
const date_fns_1 = require("date-fns");
class Task {
constructor(scheduler, name, func) {
this.matcher = new matcher_1.Matcher();
this.app = container_1.Container.get('app');
this.cornParser = new corn_parser_1.CornParser();
this.scheduler = scheduler;
this.func = func;
this.name = name;
}
match(currentDate) {
const expression = this.cornParser.interprete(this.scheduler.getExpression());
return this.matcher.match(expression, currentDate, this.scheduler.getTimezone());
}
async execute() {
if (typeof this.func !== 'function')
return;
try {
if (this.scheduler.isRunOnSingletonServer()) {
const serverShouldRun = await this.serverShouldRun();
if (serverShouldRun) {
await this.func();
}
}
else {
await this.func();
}
}
catch (err) {
this.app.emit('error', err);
}
}
async serverShouldRun() {
const hasScheduleRedis = (0, helpers_1.config)().has('redis.schedule');
const connection = hasScheduleRedis ? 'schedule' : 'default';
return (0, helpers_1.cache)('redis', connection).add(`schedule.${this.name}.${(0, date_fns_1.format)(Date.now(), 'HHmm')}`, true, 3600);
}
}
exports.Task = Task;
//# sourceMappingURL=task.js.map