From 41a8344be9d72a99e4fa8564d93c2ba844227d28 Mon Sep 17 00:00:00 2001 From: Junyi Date: Sat, 2 Mar 2024 14:23:55 +0800 Subject: [PATCH] fix(plugin-workflow): fix off static schedule trigger (#3595) * fix(plugin-workflow): fix off static schedule trigger * test(plugin-workflow): add test case --- .../triggers/schedule/mode-static.test.ts | 27 +++++++++++++++++++ .../ScheduleTrigger/StaticScheduleTrigger.ts | 11 +++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/schedule/mode-static.test.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/schedule/mode-static.test.ts index 11adb2454..0a573e88f 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/schedule/mode-static.test.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/__tests__/triggers/schedule/mode-static.test.ts @@ -218,6 +218,33 @@ describe('workflow > triggers > schedule > static mode', () => { }); }); + describe('status', () => { + it('should not trigger after turned off', async () => { + const start = await sleepToEvenSecond(); + const future = new Date(); + future.setSeconds(future.getSeconds() + 2); + + const workflow = await WorkflowModel.create({ + enabled: true, + type: 'schedule', + config: { + mode: 0, + startsOn: future.toISOString(), + repeat: 1000, + }, + }); + + await sleep(1000); + + await workflow.update({ enabled: false }); + + await sleep(3000); + + const executions = await workflow.getExecutions(); + expect(executions.length).toBe(0); + }); + }); + describe('dispatch', () => { it('missed non-repeated scheduled time should not be triggered', async () => { await sleepToEvenSecond(); diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/ScheduleTrigger/StaticScheduleTrigger.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/ScheduleTrigger/StaticScheduleTrigger.ts index 966aa3a34..fc1e622a0 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/ScheduleTrigger/StaticScheduleTrigger.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/triggers/ScheduleTrigger/StaticScheduleTrigger.ts @@ -78,8 +78,8 @@ export default class StaticScheduleTrigger { } schedule(workflow, nextTime, toggle = true) { - const key = `${workflow.id}@${nextTime}`; if (toggle) { + const key = `${workflow.id}@${nextTime}`; if (!this.timers.has(key)) { const interval = Math.max(nextTime - Date.now(), 0); if (interval > MAX_SAFE_INTERVAL) { @@ -95,9 +95,12 @@ export default class StaticScheduleTrigger { } } } else { - const timer = this.timers.get(key); - clearTimeout(timer); - this.timers.delete(key); + for (const [key, timer] of this.timers.entries()) { + if (key.startsWith(`${workflow.id}@`)) { + clearTimeout(timer); + this.timers.delete(key); + } + } } }