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:
		
							parent
							
								
									7c79e58df9
								
							
						
					
					
						commit
						41a8344be9
					
				@ -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', () => {
 | 
					  describe('dispatch', () => {
 | 
				
			||||||
    it('missed non-repeated scheduled time should not be triggered', async () => {
 | 
					    it('missed non-repeated scheduled time should not be triggered', async () => {
 | 
				
			||||||
      await sleepToEvenSecond();
 | 
					      await sleepToEvenSecond();
 | 
				
			||||||
 | 
				
			|||||||
@ -78,8 +78,8 @@ export default class StaticScheduleTrigger {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  schedule(workflow, nextTime, toggle = true) {
 | 
					  schedule(workflow, nextTime, toggle = true) {
 | 
				
			||||||
    const key = `${workflow.id}@${nextTime}`;
 | 
					 | 
				
			||||||
    if (toggle) {
 | 
					    if (toggle) {
 | 
				
			||||||
 | 
					      const key = `${workflow.id}@${nextTime}`;
 | 
				
			||||||
      if (!this.timers.has(key)) {
 | 
					      if (!this.timers.has(key)) {
 | 
				
			||||||
        const interval = Math.max(nextTime - Date.now(), 0);
 | 
					        const interval = Math.max(nextTime - Date.now(), 0);
 | 
				
			||||||
        if (interval > MAX_SAFE_INTERVAL) {
 | 
					        if (interval > MAX_SAFE_INTERVAL) {
 | 
				
			||||||
@ -95,9 +95,12 @@ export default class StaticScheduleTrigger {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      const timer = this.timers.get(key);
 | 
					      for (const [key, timer] of this.timers.entries()) {
 | 
				
			||||||
      clearTimeout(timer);
 | 
					        if (key.startsWith(`${workflow.id}@`)) {
 | 
				
			||||||
      this.timers.delete(key);
 | 
					          clearTimeout(timer);
 | 
				
			||||||
 | 
					          this.timers.delete(key);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user