fix(plugin-workflow): fix schedule repeat logic (#3612)

* fix(plugin-workflow): fix schedule repeat logic

* fix(plugin-workflow): fix test case
This commit is contained in:
Junyi 2024-03-05 21:08:46 +08:00 committed by GitHub
parent 951aece70c
commit 8e16bab213
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 32 deletions

View File

@ -119,7 +119,8 @@ describe('workflow > triggers > schedule > static mode', () => {
});
it('start before now and repeat every 2 seconds after created and limit 1', async () => {
const start = await sleepToEvenSecond();
await sleepToEvenSecond();
const start = new Date();
start.setMilliseconds(0);
const workflow = await WorkflowModel.create({
@ -166,6 +167,68 @@ describe('workflow > triggers > schedule > static mode', () => {
expect(date.getTime()).toBe(now.getTime());
});
it('no repeat triggered then update to repeat', async () => {
const start = await sleepToEvenSecond();
const workflow = await WorkflowModel.create({
enabled: true,
type: 'schedule',
config: {
mode: 0,
startsOn: start.toISOString(),
},
});
await sleep(1000);
const e1s = await workflow.getExecutions();
expect(e1s.length).toBe(0);
await workflow.update({
config: {
...workflow.config,
repeat: 1000,
},
});
console.log(new Date().toISOString());
await sleep(3000);
const e2s = await workflow.getExecutions();
console.log(e2s);
expect(e2s.length).toBe(2);
});
});
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('multiple workflows trigger at same time', async () => {
const now = new Date();
const startsOn = now.toISOString();
@ -216,36 +279,7 @@ describe('workflow > triggers > schedule > static mode', () => {
d2.setMilliseconds(0);
expect(d2.getTime()).toBe(now.getTime());
});
});
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

@ -27,7 +27,6 @@ export default class StaticScheduleTrigger {
inspect(workflows) {
const now = new Date();
now.setMilliseconds(0);
workflows.forEach((workflow) => {
const nextTime = this.getNextTime(workflow, now);
@ -65,7 +64,8 @@ export default class StaticScheduleTrigger {
const next = interval.next();
return next.getTime();
} else if (typeof config.repeat === 'number') {
return timestamp + ((timestamp - startTime) % config.repeat);
const next = timestamp + config.repeat - ((timestamp - startTime) % config.repeat);
return next;
} else {
return null;
}