-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Schedule Trigger Node): Change scheduler behaviour for intervals …
…days and hours (#5133) * 🐛Fix scheduler for intervals days and week * ♻️ Simplify and move recurrency rules outside trigger node * Remove async and promise from recurency rule * Update correctly the Static data when using recurrency Rule * Fix logic when recurrency is activated * 🎨 Remove useless staticData fix(passed by reference) * 🐛 remove duplicted hour cronJob leading to 2 executions * More fixes, handles multiple execution * 🐛 fixing dayOfYear recurency check * 🐛 fix recurency check for hours/days should not equal lastExecution * Add month interval to the scheduler * Fix flawed logic for comparing interval * 🚨 Fix lint issue type --------- Co-authored-by: Marcus <marcus@n8n.io>
- Loading branch information
Showing
3 changed files
with
181 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { IRecurencyRule } from './SchedulerInterface'; | ||
import moment from 'moment'; | ||
|
||
export function recurencyCheck( | ||
recurrency: IRecurencyRule, | ||
recurrencyRules: number[], | ||
timezone: string, | ||
): boolean { | ||
const recurrencyRuleIndex = recurrency.index; | ||
const intervalSize = recurrency.intervalSize; | ||
const typeInterval = recurrency.typeInterval; | ||
|
||
const lastExecution = | ||
recurrencyRuleIndex !== undefined ? recurrencyRules[recurrencyRuleIndex] : undefined; | ||
|
||
if ( | ||
intervalSize && | ||
recurrencyRuleIndex !== undefined && | ||
(typeInterval === 'weeks' || typeInterval === 'undefined') | ||
) { | ||
if ( | ||
lastExecution === undefined || // First time executing this rule | ||
moment.tz(timezone).week() === (intervalSize + lastExecution) % 52 || // not first time, but minimum interval has passed | ||
moment.tz(timezone).week() === lastExecution // Trigger on multiple days in the same week | ||
) { | ||
recurrencyRules[recurrencyRuleIndex] = moment.tz(timezone).week(); | ||
return true; | ||
} | ||
} else if (intervalSize && recurrencyRuleIndex !== undefined && typeInterval === 'days') { | ||
if ( | ||
lastExecution === undefined || | ||
moment.tz(timezone).dayOfYear() === (intervalSize + lastExecution) % 365 | ||
) { | ||
recurrencyRules[recurrencyRuleIndex] = moment.tz(timezone).dayOfYear(); | ||
return true; | ||
} | ||
} else if (intervalSize && recurrencyRuleIndex !== undefined && typeInterval === 'hours') { | ||
if ( | ||
lastExecution === undefined || | ||
moment.tz(timezone).hour() === (intervalSize + lastExecution) % 24 | ||
) { | ||
recurrencyRules[recurrencyRuleIndex] = moment.tz(timezone).hour(); | ||
return true; | ||
} | ||
} else if (intervalSize && recurrencyRuleIndex !== undefined && typeInterval === 'months') { | ||
if ( | ||
lastExecution === undefined || | ||
moment.tz(timezone).month() === (intervalSize + lastExecution) % 12 | ||
) { | ||
recurrencyRules[recurrencyRuleIndex] = moment.tz(timezone).month(); | ||
return true; | ||
} | ||
} else { | ||
return true; | ||
} | ||
return false; | ||
} |
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,6 @@ | ||
export interface IRecurencyRule { | ||
activated: boolean; | ||
index?: number; | ||
intervalSize?: number; | ||
typeInterval?: string; | ||
} |