forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Centralize CronJob management (n8n-io#10033)
- Loading branch information
Showing
18 changed files
with
725 additions
and
424 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { Workflow } from 'n8n-workflow'; | ||
import { mock } from 'jest-mock-extended'; | ||
|
||
import { ScheduledTaskManager } from '@/ScheduledTaskManager'; | ||
|
||
describe('ScheduledTaskManager', () => { | ||
const workflow = mock<Workflow>({ timezone: 'GMT' }); | ||
const everyMinute = '0 * * * * *'; | ||
const onTick = jest.fn(); | ||
|
||
let scheduledTaskManager: ScheduledTaskManager; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
jest.useFakeTimers(); | ||
scheduledTaskManager = new ScheduledTaskManager(); | ||
}); | ||
|
||
it('should throw when workflow timezone is invalid', () => { | ||
expect(() => | ||
scheduledTaskManager.registerCron( | ||
mock<Workflow>({ timezone: 'somewhere' }), | ||
everyMinute, | ||
onTick, | ||
), | ||
).toThrow('Invalid timezone.'); | ||
}); | ||
|
||
it('should throw when cron expression is invalid', () => { | ||
expect(() => | ||
//@ts-expect-error invalid cron expression is a type-error | ||
scheduledTaskManager.registerCron(workflow, 'invalid-cron-expression', onTick), | ||
).toThrow(); | ||
}); | ||
|
||
it('should register valid CronJobs', async () => { | ||
scheduledTaskManager.registerCron(workflow, everyMinute, onTick); | ||
|
||
expect(onTick).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(10 * 60 * 1000); // 10 minutes | ||
expect(onTick).toHaveBeenCalledTimes(10); | ||
}); | ||
|
||
it('should deregister CronJobs for a workflow', async () => { | ||
scheduledTaskManager.registerCron(workflow, everyMinute, onTick); | ||
scheduledTaskManager.registerCron(workflow, everyMinute, onTick); | ||
scheduledTaskManager.registerCron(workflow, everyMinute, onTick); | ||
scheduledTaskManager.deregisterCrons(workflow.id); | ||
|
||
expect(onTick).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(10 * 60 * 1000); // 10 minutes | ||
expect(onTick).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.