mirror of
https://github.com/certd/certd.git
synced 2026-04-23 19:57:27 +08:00
fix: 修复在相同的cron时偶尔无法触发定时任务的bug
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import cron from 'node-cron';
|
||||
export type CronTask = {
|
||||
import parser from 'cron-parser';
|
||||
import { ILogger } from '@certd/pipeline';
|
||||
|
||||
export type CronTaskReq = {
|
||||
/**
|
||||
* 为空则为单次执行
|
||||
*/
|
||||
@@ -7,39 +9,90 @@ export type CronTask = {
|
||||
job: () => Promise<void>;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export class CronTask {
|
||||
logger: ILogger;
|
||||
cron: string;
|
||||
job: () => Promise<void>;
|
||||
name: string;
|
||||
stoped = false;
|
||||
|
||||
timeoutId: any;
|
||||
|
||||
constructor(req: CronTaskReq, logger: ILogger) {
|
||||
this.cron = req.cron;
|
||||
this.job = req.job;
|
||||
this.name = req.name;
|
||||
this.logger = logger;
|
||||
this.start();
|
||||
}
|
||||
|
||||
start() {
|
||||
if (!this.cron) {
|
||||
return;
|
||||
}
|
||||
if (this.stoped) {
|
||||
return;
|
||||
}
|
||||
const interval = parser.parseExpression(this.cron);
|
||||
const next = interval.next().getTime();
|
||||
const now = Date.now();
|
||||
const delay = next - now;
|
||||
this.timeoutId = setTimeout(async () => {
|
||||
try {
|
||||
if (this.stoped) {
|
||||
return;
|
||||
}
|
||||
await this.job();
|
||||
} catch (e) {
|
||||
this.logger.error(`[cron] job error : [${this.name}]`, e);
|
||||
}
|
||||
this.start();
|
||||
}, delay);
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.stoped = true;
|
||||
clearTimeout(this.timeoutId);
|
||||
}
|
||||
}
|
||||
export class Cron {
|
||||
logger;
|
||||
logger: ILogger;
|
||||
immediateTriggerOnce: boolean;
|
||||
constructor(opts) {
|
||||
|
||||
bucket: Record<string, CronTask> = {};
|
||||
|
||||
constructor(opts: any) {
|
||||
this.logger = opts.logger;
|
||||
this.immediateTriggerOnce = opts.immediateTriggerOnce;
|
||||
}
|
||||
|
||||
register(task: CronTask) {
|
||||
if (!task.cron) {
|
||||
this.logger.info(`[cron] register once : [${task.name}]`);
|
||||
task.job();
|
||||
register(req: CronTaskReq) {
|
||||
if (!req.cron) {
|
||||
this.logger.info(`[cron] register once : [${req.name}]`);
|
||||
req.job().catch(e => {
|
||||
this.logger.error(`job execute error : [${req.name}]`, e);
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.logger.info(`[cron] register cron : [${task.name}] ,${task.cron}`);
|
||||
cron.schedule(task.cron, task.job, {
|
||||
name: task.name,
|
||||
});
|
||||
this.logger.info('当前定时任务数量:', this.getListSize());
|
||||
this.logger.info(`[cron] register cron : [${req.name}] ,${req.cron}`);
|
||||
|
||||
const task = new CronTask(req, this.logger);
|
||||
this.bucket[task.name] = task;
|
||||
this.logger.info('当前定时任务数量:', this.getTaskSize());
|
||||
}
|
||||
|
||||
remove(taskName: string) {
|
||||
this.logger.info(`[cron] remove : [${taskName}]`);
|
||||
const tasks = cron.getTasks() as Map<any, any>;
|
||||
const node = tasks.get(taskName);
|
||||
if (node) {
|
||||
node.stop();
|
||||
tasks.delete(taskName);
|
||||
const task = this.bucket[taskName];
|
||||
if (task) {
|
||||
task.stop();
|
||||
delete this.bucket[taskName];
|
||||
}
|
||||
}
|
||||
|
||||
getListSize() {
|
||||
const tasks = cron.getTasks();
|
||||
return tasks.size;
|
||||
getTaskSize() {
|
||||
const tasks = Object.keys(this.bucket);
|
||||
return tasks.length;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user