mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
refactor(core): Centralize CronJob management (#10033)
This commit is contained in:
committed by
GitHub
parent
36b314d031
commit
09f2cf9eaf
31
packages/core/src/ScheduledTaskManager.ts
Normal file
31
packages/core/src/ScheduledTaskManager.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Service } from 'typedi';
|
||||
import { CronJob } from 'cron';
|
||||
import type { CronExpression, Workflow } from 'n8n-workflow';
|
||||
|
||||
@Service()
|
||||
export class ScheduledTaskManager {
|
||||
readonly cronJobs = new Map<string, CronJob[]>();
|
||||
|
||||
registerCron(workflow: Workflow, cronExpression: CronExpression, onTick: () => void) {
|
||||
const cronJob = new CronJob(cronExpression, onTick, undefined, true, workflow.timezone);
|
||||
const cronJobsForWorkflow = this.cronJobs.get(workflow.id);
|
||||
if (cronJobsForWorkflow) {
|
||||
cronJobsForWorkflow.push(cronJob);
|
||||
} else {
|
||||
this.cronJobs.set(workflow.id, [cronJob]);
|
||||
}
|
||||
}
|
||||
|
||||
deregisterCrons(workflowId: string) {
|
||||
const cronJobs = this.cronJobs.get(workflowId) ?? [];
|
||||
for (const cronJob of cronJobs) {
|
||||
cronJob.stop();
|
||||
}
|
||||
}
|
||||
|
||||
deregisterAllCrons() {
|
||||
for (const workflowId of Object.keys(this.cronJobs)) {
|
||||
this.deregisterCrons(workflowId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user