fix(Schedule Trigger Node): Default to 0 minute if falsy on hourly run (#9146)

This commit is contained in:
Michael Kret 2024-04-18 12:57:36 +03:00 committed by GitHub
parent 1c7acbb629
commit d756609826
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 8 deletions

View file

@ -81,3 +81,13 @@ export function convertToUnixFormat(interval: IDataObject) {
} }
interval.expression = expression.join(' '); interval.expression = expression.join(' ');
} }
export const addFallbackValue = <T>(enabled: boolean, fallback: T) => {
if (enabled) {
return (value: T) => {
if (!value) return fallback;
return value;
};
}
return (value: T) => value;
};

View file

@ -10,7 +10,7 @@ import { NodeOperationError } from 'n8n-workflow';
import { CronJob } from 'cron'; import { CronJob } from 'cron';
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import type { IRecurencyRule } from './SchedulerInterface'; import type { IRecurencyRule } from './SchedulerInterface';
import { convertToUnixFormat, recurencyCheck } from './GenericFunctions'; import { addFallbackValue, convertToUnixFormat, recurencyCheck } from './GenericFunctions';
export class ScheduleTrigger implements INodeType { export class ScheduleTrigger implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -18,7 +18,7 @@ export class ScheduleTrigger implements INodeType {
name: 'scheduleTrigger', name: 'scheduleTrigger',
icon: 'fa:clock', icon: 'fa:clock',
group: ['trigger', 'schedule'], group: ['trigger', 'schedule'],
version: [1, 1.1], version: [1, 1.1, 1.2],
description: 'Triggers the workflow on a given schedule', description: 'Triggers the workflow on a given schedule',
eventTriggerDescription: '', eventTriggerDescription: '',
activationMessage: activationMessage:
@ -415,7 +415,7 @@ 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 version = this.getNode().typeVersion; const nodeVersion = this.getNode().typeVersion;
const cronJobs: CronJob[] = []; const cronJobs: CronJob[] = [];
const intervalArr: NodeJS.Timeout[] = []; const intervalArr: NodeJS.Timeout[] = [];
const staticData = this.getWorkflowStaticData('node') as { const staticData = this.getWorkflowStaticData('node') as {
@ -424,6 +424,7 @@ export class ScheduleTrigger implements INodeType {
if (!staticData.recurrencyRules) { if (!staticData.recurrencyRules) {
staticData.recurrencyRules = []; staticData.recurrencyRules = [];
} }
const fallbackToZero = addFallbackValue(nodeVersion >= 1.2, '0');
const executeTrigger = async (recurency: IRecurencyRule) => { const executeTrigger = async (recurency: IRecurencyRule) => {
const resultData = { const resultData = {
timestamp: moment.tz(timezone).toISOString(true), timestamp: moment.tz(timezone).toISOString(true),
@ -451,7 +452,7 @@ export class ScheduleTrigger implements INodeType {
for (let i = 0; i < interval.length; i++) { for (let i = 0; i < interval.length; i++) {
let intervalValue = 1000; let intervalValue = 1000;
if (interval[i].field === 'cronExpression') { if (interval[i].field === 'cronExpression') {
if (version > 1) { if (nodeVersion > 1) {
// ! Remove this part if we use a cron library that follows unix cron expression // ! Remove this part if we use a cron library that follows unix cron expression
convertToUnixFormat(interval[i]); convertToUnixFormat(interval[i]);
} }
@ -494,7 +495,8 @@ export class ScheduleTrigger implements INodeType {
if (interval[i].field === 'hours') { if (interval[i].field === 'hours') {
const hour = interval[i].hoursInterval as number; const hour = interval[i].hoursInterval as number;
const minute = interval[i].triggerAtMinute?.toString() as string; const minute = fallbackToZero(interval[i].triggerAtMinute?.toString() as string);
const cronTimes: string[] = [minute, '*', '*', '*', '*']; const cronTimes: string[] = [minute, '*', '*', '*', '*'];
const cronExpression: string = cronTimes.join(' '); const cronExpression: string = cronTimes.join(' ');
if (hour === 1) { if (hour === 1) {
@ -527,7 +529,7 @@ export class ScheduleTrigger implements INodeType {
if (interval[i].field === 'days') { if (interval[i].field === 'days') {
const day = interval[i].daysInterval as number; const day = interval[i].daysInterval as number;
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 = fallbackToZero(interval[i].triggerAtMinute?.toString() as string);
const cronTimes: string[] = [minute, hour, '*', '*', '*']; const cronTimes: string[] = [minute, hour, '*', '*', '*'];
const cronExpression: string = cronTimes.join(' '); const cronExpression: string = cronTimes.join(' ');
if (day === 1) { if (day === 1) {
@ -559,7 +561,7 @@ export class ScheduleTrigger implements INodeType {
if (interval[i].field === 'weeks') { if (interval[i].field === 'weeks') {
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 = fallbackToZero(interval[i].triggerAtMinute?.toString() as string);
const week = interval[i].weeksInterval as number; const week = interval[i].weeksInterval as number;
const days = interval[i].triggerAtDay as IDataObject[]; const days = interval[i].triggerAtDay as IDataObject[];
const day = days.length === 0 ? '*' : days.join(','); const day = days.length === 0 ? '*' : days.join(',');
@ -596,7 +598,7 @@ export class ScheduleTrigger implements INodeType {
const month = interval[i].monthsInterval; const month = interval[i].monthsInterval;
const day = interval[i].triggerAtDayOfMonth?.toString() as string; const day = interval[i].triggerAtDayOfMonth?.toString() 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 = fallbackToZero(interval[i].triggerAtMinute?.toString() as string);
const cronTimes: string[] = [minute, hour, day, '*', '*']; const cronTimes: string[] = [minute, hour, day, '*', '*'];
const cronExpression: string = cronTimes.join(' '); const cronExpression: string = cronTimes.join(' ');
if (month === 1) { if (month === 1) {