blue-fish-redis
Version:
这是一个修复漏洞后的redis
49 lines (48 loc) • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisCron = void 0;
const blue_fish_helper_1 = require("blue-fish-helper");
const coa_echo_1 = require("coa-echo");
const CronTime_1 = require("./CronTime");
const D = { series: 0 };
class RedisCron {
constructor(worker, version) {
this.times = {};
this.workers = {};
this.pusher = worker.on('CRON', async (id) => await this.work(id));
this.key_cron_last = worker.queue.keys.prefix + 'cron-last';
this.version = version || '';
this.io = worker.queue.bin.io;
}
// 添加日程计划
on(time, worker) {
const id = `${this.version}-${++D.series}`;
this.times[id] = time;
this.workers[id] = worker;
}
// 尝试触发
async try() {
const deadline = blue_fish_helper_1._.now();
const start = blue_fish_helper_1._.toInteger(await this.io.getset(this.key_cron_last, deadline)) || deadline - 1000;
blue_fish_helper_1._.forEach(this.times, (time, id) => {
const next = new CronTime_1.CronTime(time, { start, deadline }).next();
next && this.pusher(id, {});
});
}
// 开始执行
async work(id) {
const worker = this.workers[id];
if (worker) {
try {
await worker();
}
catch (e) {
coa_echo_1.echo.error('* Cron JobError: %s %s', id, this.times[id], e.toString());
}
}
else {
coa_echo_1.echo.error('* Cron JobNotFound: %s %s', id, this.times[id]);
}
}
}
exports.RedisCron = RedisCron;