fix(plugin-workflow): fix off static schedule trigger (#3595)

* fix(plugin-workflow): fix off static schedule trigger

* test(plugin-workflow): add test case
This commit is contained in:
Junyi 2024-03-02 14:23:55 +08:00 committed by GitHub
parent 7c79e58df9
commit 41a8344be9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 4 deletions

View File

@ -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();

View File

@ -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);
}
}
}
}