Skip to content

Commit

Permalink
fix(Shedule Node): fixes multiple intervals, fixes week interval (#4376)
Browse files Browse the repository at this point in the history
* 🐛 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
  • Loading branch information
agobrech authored Oct 19, 2022
1 parent 77d041b commit 971c2c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
2 changes: 0 additions & 2 deletions packages/nodes-base/nodes/Schedule/CronInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { IDataObject } from 'n8n-workflow';

export type ICronExpression = [
string | Date,
string | Date,
Expand Down
33 changes: 18 additions & 15 deletions packages/nodes-base/nodes/Schedule/ScheduleTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ export class ScheduleTrigger implements INodeType {
},
{
name: 'Sunday',
value: 7,
value: 0,
},
],
default: [7],
default: [0],
},
{
displayName: 'Trigger at Hour',
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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() {
Expand Down

0 comments on commit 971c2c0

Please sign in to comment.