mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 12:57:29 -08:00
fix(Schedule Trigger Node): Follow the correct Unix cron format for month and days of the week (#6401)
* Handle conversion to correct unix format * Fix intervals, ranges for months * fix regex to match 10, 11, 12 --------- Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
parent
75c0ab03f8
commit
2aef9de148
|
@ -1,3 +1,4 @@
|
||||||
|
import type { IDataObject } from 'n8n-workflow';
|
||||||
import type { IRecurencyRule } from './SchedulerInterface';
|
import type { IRecurencyRule } from './SchedulerInterface';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
|
@ -55,3 +56,28 @@ export function recurencyCheck(
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function convertMonthToUnix(expression: string): string {
|
||||||
|
if (!isNaN(parseInt(expression)) || expression.includes('-') || expression.includes(',')) {
|
||||||
|
let matches = expression.match(/([0-9])+/g) as string[];
|
||||||
|
if (matches) {
|
||||||
|
matches = matches.map((match) =>
|
||||||
|
parseInt(match) !== 0 ? String(parseInt(match) - 1) : match,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
expression = matches?.join(expression.includes('-') ? '-' : ',') || '';
|
||||||
|
}
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function convertToUnixFormat(interval: IDataObject) {
|
||||||
|
const expression = (interval.expression as string).split(' ');
|
||||||
|
if (expression.length === 5) {
|
||||||
|
expression[3] = convertMonthToUnix(expression[3]);
|
||||||
|
expression[4] = expression[4].replace('7', '0');
|
||||||
|
} else if (expression.length === 6) {
|
||||||
|
expression[4] = convertMonthToUnix(expression[4]);
|
||||||
|
expression[5] = expression[5].replace('7', '0');
|
||||||
|
}
|
||||||
|
interval.expression = expression.join(' ');
|
||||||
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { NodeOperationError } from 'n8n-workflow';
|
||||||
import { CronJob } from 'cron';
|
import { CronJob } from 'cron';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import type { IRecurencyRule } from './SchedulerInterface';
|
import type { IRecurencyRule } from './SchedulerInterface';
|
||||||
import { recurencyCheck } from './GenericFunctions';
|
import { 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,
|
version: [1, 1.1],
|
||||||
description: 'Triggers the workflow on a given schedule',
|
description: 'Triggers the workflow on a given schedule',
|
||||||
eventTriggerDescription: '',
|
eventTriggerDescription: '',
|
||||||
activationMessage:
|
activationMessage:
|
||||||
|
@ -415,6 +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 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 {
|
||||||
|
@ -450,6 +451,10 @@ 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) {
|
||||||
|
// ! Remove this part if we use a cron library that follows unix cron expression
|
||||||
|
convertToUnixFormat(interval[i]);
|
||||||
|
}
|
||||||
const cronExpression = interval[i].expression as string;
|
const cronExpression = interval[i].expression as string;
|
||||||
try {
|
try {
|
||||||
const cronJob = new CronJob(
|
const cronJob = new CronJob(
|
||||||
|
|
Loading…
Reference in a new issue