From 971c2c0aed0a2402fa6eb6217ddc914d1fade68b Mon Sep 17 00:00:00 2001 From: agobrech <45268029+agobrech@users.noreply.github.com> Date: Wed, 19 Oct 2022 17:28:41 +0200 Subject: [PATCH] fix(Shedule Node): fixes multiple intervals, fixes week interval (#4376) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 fix bug where adding multiple intervals would remove the previous one * 🐛 swap hour logic with week logic, change sunday to 0 value * 🔥 remove console.logs * 🔥 remove unused variable * 🔥 remove unused imports --- .../nodes/Schedule/CronInterface.ts | 2 -- .../nodes/Schedule/ScheduleTrigger.node.ts | 33 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/nodes-base/nodes/Schedule/CronInterface.ts b/packages/nodes-base/nodes/Schedule/CronInterface.ts index 22b6473cd1b7b..5e1521d6da229 100644 --- a/packages/nodes-base/nodes/Schedule/CronInterface.ts +++ b/packages/nodes-base/nodes/Schedule/CronInterface.ts @@ -1,5 +1,3 @@ -import { IDataObject } from 'n8n-workflow'; - export type ICronExpression = [ string | Date, string | Date, diff --git a/packages/nodes-base/nodes/Schedule/ScheduleTrigger.node.ts b/packages/nodes-base/nodes/Schedule/ScheduleTrigger.node.ts index 7b76bddbe625c..9334b9600bf72 100644 --- a/packages/nodes-base/nodes/Schedule/ScheduleTrigger.node.ts +++ b/packages/nodes-base/nodes/Schedule/ScheduleTrigger.node.ts @@ -223,10 +223,10 @@ export class ScheduleTrigger implements INodeType { }, { name: 'Sunday', - value: 7, + value: 0, }, ], - default: [7], + default: [0], }, { displayName: 'Trigger at Hour', @@ -414,9 +414,8 @@ export class ScheduleTrigger implements INodeType { const rule = this.getNodeParameter('rule', []) as IDataObject; const interval = rule.interval as IDataObject[]; const timezone = this.getTimezone(); - const date = moment.tz(timezone).week(); const cronJobs: CronJob[] = []; - let intervalObj: NodeJS.Timeout; + const intervalArr: NodeJS.Timeout[] = []; const executeTrigger = () => { const resultData = { timestamp: moment.tz(timezone).toISOString(true), @@ -450,21 +449,22 @@ export class ScheduleTrigger implements INodeType { if (interval[i].field === 'seconds') { const seconds = interval[i].secondsInterval as number; intervalValue *= seconds; - intervalObj = setInterval(executeTrigger, intervalValue); + const intervalObj = setInterval(executeTrigger, intervalValue) as NodeJS.Timeout; + intervalArr.push(intervalObj); } if (interval[i].field === 'minutes') { const minutes = interval[i].minutesInterval as number; intervalValue *= 60 * minutes; - intervalObj = setInterval(executeTrigger, intervalValue); + const intervalObj = setInterval(executeTrigger, intervalValue); + intervalArr.push(intervalObj); } if (interval[i].field === 'hours') { - const hour = interval[i].triggerAtHour?.toString() as string; + const hour = interval[i].hoursInterval?.toString() as string; const minute = interval[i].triggerAtMinute?.toString() as string; - const week = interval[i].triggerAtWeek as number; - const cronTimes: ICronExpression = [minute, hour, `*/${week * 7}`, '*', '*']; - const cronExpression = cronTimes.join(' '); + const cronTimes: ICronExpression = [minute, `*/${hour}`, '*', '*', '*']; + const cronExpression: string = cronTimes.join(' '); const cronJob = new CronJob(cronExpression, executeTrigger, undefined, true, timezone); cronJobs.push(cronJob); } @@ -480,12 +480,13 @@ export class ScheduleTrigger implements INodeType { } if (interval[i].field === 'weeks') { - const days = interval[i].triggerAtDay as IDataObject[]; - const day = days.join(',') as string; const hour = interval[i].triggerAtHour?.toString() as string; const minute = interval[i].triggerAtMinute?.toString() as string; - const cronTimes: ICronExpression = [minute, hour, '*', '*', day]; - const cronExpression: string = cronTimes.join(' '); + const week = interval[i].weeksInterval as number; + const days = interval[i].triggerAtDay as IDataObject[]; + const day = days.length === 0 ? '*' : (days.join(',') as string); + const cronTimes: ICronExpression = [minute, hour, `*/${week * 7}`, '*', day]; + const cronExpression = cronTimes.join(' '); const cronJob = new CronJob(cronExpression, executeTrigger, undefined, true, timezone); cronJobs.push(cronJob); } @@ -506,7 +507,9 @@ export class ScheduleTrigger implements INodeType { for (const cronJob of cronJobs) { cronJob.stop(); } - clearInterval(intervalObj); + for (const interval of intervalArr) { + clearInterval(interval); + } } async function manualTriggerFunction() {