fix(Shedule Node): fixes multiple intervals, fixes week interval (#4376)

* 🐛 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
This commit is contained in:
agobrech 2022-10-19 17:28:41 +02:00 committed by GitHub
parent 77d041ba78
commit 971c2c0aed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 17 deletions

View file

@ -1,5 +1,3 @@
import { IDataObject } from 'n8n-workflow';
export type ICronExpression = [ export type ICronExpression = [
string | Date, string | Date,
string | Date, string | Date,

View file

@ -223,10 +223,10 @@ export class ScheduleTrigger implements INodeType {
}, },
{ {
name: 'Sunday', name: 'Sunday',
value: 7, value: 0,
}, },
], ],
default: [7], default: [0],
}, },
{ {
displayName: 'Trigger at Hour', displayName: 'Trigger at Hour',
@ -414,9 +414,8 @@ export class ScheduleTrigger implements INodeType {
const rule = this.getNodeParameter('rule', []) as IDataObject; const rule = this.getNodeParameter('rule', []) as IDataObject;
const interval = rule.interval as IDataObject[]; const interval = rule.interval as IDataObject[];
const timezone = this.getTimezone(); const timezone = this.getTimezone();
const date = moment.tz(timezone).week();
const cronJobs: CronJob[] = []; const cronJobs: CronJob[] = [];
let intervalObj: NodeJS.Timeout; const intervalArr: NodeJS.Timeout[] = [];
const executeTrigger = () => { const executeTrigger = () => {
const resultData = { const resultData = {
timestamp: moment.tz(timezone).toISOString(true), timestamp: moment.tz(timezone).toISOString(true),
@ -450,21 +449,22 @@ export class ScheduleTrigger implements INodeType {
if (interval[i].field === 'seconds') { if (interval[i].field === 'seconds') {
const seconds = interval[i].secondsInterval as number; const seconds = interval[i].secondsInterval as number;
intervalValue *= seconds; intervalValue *= seconds;
intervalObj = setInterval(executeTrigger, intervalValue); const intervalObj = setInterval(executeTrigger, intervalValue) as NodeJS.Timeout;
intervalArr.push(intervalObj);
} }
if (interval[i].field === 'minutes') { if (interval[i].field === 'minutes') {
const minutes = interval[i].minutesInterval as number; const minutes = interval[i].minutesInterval as number;
intervalValue *= 60 * minutes; intervalValue *= 60 * minutes;
intervalObj = setInterval(executeTrigger, intervalValue); const intervalObj = setInterval(executeTrigger, intervalValue);
intervalArr.push(intervalObj);
} }
if (interval[i].field === 'hours') { 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 minute = interval[i].triggerAtMinute?.toString() as string;
const week = interval[i].triggerAtWeek as number; const cronTimes: ICronExpression = [minute, `*/${hour}`, '*', '*', '*'];
const cronTimes: ICronExpression = [minute, hour, `*/${week * 7}`, '*', '*']; const cronExpression: string = cronTimes.join(' ');
const cronExpression = cronTimes.join(' ');
const cronJob = new CronJob(cronExpression, executeTrigger, undefined, true, timezone); const cronJob = new CronJob(cronExpression, executeTrigger, undefined, true, timezone);
cronJobs.push(cronJob); cronJobs.push(cronJob);
} }
@ -480,12 +480,13 @@ export class ScheduleTrigger implements INodeType {
} }
if (interval[i].field === 'weeks') { 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 hour = interval[i].triggerAtHour?.toString() as string;
const minute = interval[i].triggerAtMinute?.toString() as string; const minute = interval[i].triggerAtMinute?.toString() as string;
const cronTimes: ICronExpression = [minute, hour, '*', '*', day]; const week = interval[i].weeksInterval as number;
const cronExpression: string = cronTimes.join(' '); 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); const cronJob = new CronJob(cronExpression, executeTrigger, undefined, true, timezone);
cronJobs.push(cronJob); cronJobs.push(cronJob);
} }
@ -506,7 +507,9 @@ export class ScheduleTrigger implements INodeType {
for (const cronJob of cronJobs) { for (const cronJob of cronJobs) {
cronJob.stop(); cronJob.stop();
} }
clearInterval(intervalObj); for (const interval of intervalArr) {
clearInterval(interval);
}
} }
async function manualTriggerFunction() { async function manualTriggerFunction() {