n8n/packages/nodes-base/nodes/Cron/Cron.node.ts

347 lines
7.8 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import { ITriggerFunctions } from 'n8n-core';
import {
INodeType,
INodeTypeDescription,
ITriggerResponse,
} from 'n8n-workflow';
import { CronJob } from 'cron';
interface TriggerTime {
mode: string;
hour: number;
minute: number;
dayOfMonth: number;
weekeday: number;
[key: string]: string | number;
}
export class Cron implements INodeType {
description: INodeTypeDescription = {
displayName: 'Cron',
name: 'cron',
icon: 'fa:calendar',
fix(editor): Fix i18n issues (#3072) * :bug: Fix `defaultLocale` watcher * :zap: Improve error handling for headers * :pencil2: Improve naming * :bug: Fix hiring banner check * :zap: Flatten base text keys * :zap: Fix miscorrected key * :zap: Implement pluralization * :pencil2: Update docs * :truck: Move headers fetching to `App.vue` * fix hiring banner * :zap: Fix missing import * :pencil2: Alphabetize translations * :zap: Switch to async check * feat(editor): Refactor Output Panel + fix i18n issues (#3097) * update main panel * finish up tabs * fix docs link * add icon * update node settings * clean up settings * add rename modal * fix component styles * fix spacing * truncate name * remove mixin * fix spacing * fix spacing * hide docs url * fix bug * fix renaming * refactor tabs out * refactor execute button * refactor header * add more views * fix error view * fix workflow rename bug * rename component * fix small screen bug * move items, fix positions * add hover state * show selector on empty state * add empty run state * fix binary view * 1 item * add vjs styles * show empty row for every item * refactor tabs * add branch names * fix spacing * fix up spacing * add run selector * fix positioning * clean up * increase width of selector * fix up spacing * fix copy button * fix branch naming; type issues * fix docs in custom nodes * add type * hide items when run selector is shown * increase selector size * add select prepend * clean up a bit * Add pagination * add stale icon * enable stale data in execution run * Revert "enable stale data in execution run" 8edb68dbffa0aa0d8189117e1a53381cb2c27608 * move metadata to its own state * fix smaller size * add scroll buttons * update tabs on resize * update stale data on rename * remove metadata on delete * hide x * change title colors * binary data classes * remove duplicate css * add colors * delete unused keys * use event bus * update styles of pagination * fix ts issues * fix ts issues * use chevron icons * fix design with download button * add back to canvas button * add trigger warning disabled * show trigger warning tooltip * update button labels for triggers * update node output message * fix add-option bug * add page selector * fix pagination selector bug * fix executions bug * remove hint * add json colors * add colors for json * add color json keys * fix select options bug * update keys * address comments * update name limit * align pencil * update icon size * update radio buttons height * address comments * fix pencil bug * change buttons alignment * fully center * change order of buttons * add no output message in branch * scroll to top * change active state * fix page size * all items * update expression background * update naming * align pencil * update modal background * add schedule group * update schedule nodes messages * use ellpises for last chars * fix spacing * fix tabs issue * fix too far data bug * fix executions bug * fix table wrapping * fix rename bug * add padding * handle unkown errors * add sticky header * ignore empty input, trim node name * nudge lightness of color * center buttons * update pagination * set colors of title * increase table font, fix alignment * fix pencil bug * fix spacing * use date now * address pagination issues * delete unused keys * update keys sort * fix prepend * fix radio button position * Revert "fix radio button position" ae42781786f2e6dcfb00d1be770b19a67f533bdf Co-authored-by: Mutasem <mutdmour@gmail.com> Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com>
2022-04-11 06:12:13 -07:00
group: ['trigger', 'schedule'],
2019-06-23 03:35:23 -07:00
version: 1,
description: 'Triggers the workflow at a specific time',
eventTriggerDescription: '',
:sparkles: Improve workflow activation (#2692) * feat: activator disabled based on thiggers * feat: tooltip over inactive switch * feat: message for trigger types * feat: deactivate on save if trigger is removed * chore: refactor executions modal * feat: calculate service name if possible * feat: alert on activation * chore: fix linting * feat: always enable activator when active * fix: adjust the alert * feat: take disabled state into account * feat: automatically save on activation * feat: rely on nodes name and edit messages * feat: isolate state for each activator instance * feat: create activation modal component * feat: activationModal checkbox and trigger message * feat: add activation messages to node config * chore: style activation modal * chore: style fixes * feat: refactor disabled state * chore: refactor modal * chore: refactor modal * chore: tidy the node config * chore: refactor and styling tweaks * chore: minor fixes * fix: check webhooks from ui nodes * chore: remove saving prompt * chore: explicit current workflow evaluation * feat: add settings link to activation modal * fix: immediately load executions on render * feat: exclude error trigger from trigger nodes * chore: add i18n keys * fix: check localstorage more strictly * fix: handle refresh in execution list * remove unnessary event * remove comment * fix closing executions modal bugs * update closing * update translation key * fix translation keys * fix modal closing * fix closing * fix drawer closing * close all modals when opening executions * update key * close all modals when opening workflow or new page * delete unnessary comment * clean up import * clean up unnessary initial data * clean up activator impl * rewrite * fix open modal bug * simply remove error * refactor activation logic * fix i18n and such * remove changes * revert saving changes * Revert "revert saving changes" 25c29d10553ebcc11939ff29938e8a5ac6b3ffae * add translation * fix new workflows saving * clean up modal impl * clean up impl * refactor common code out * remove active changes from saving * refactor differently * revert unnessary change * set dirty false * fix i18n bug * avoid opening two modals * fix tooltips * add comment * address other comments * address comments Co-authored-by: saintsebastian <tilitidam@gmail.com>
2022-01-21 09:00:00 -08:00
activationMessage: 'Your cron trigger will now trigger executions on the schedule you have defined.',
2019-06-23 03:35:23 -07:00
defaults: {
name: 'Cron',
color: '#00FF00',
},
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Trigger Times',
name: 'triggerTimes',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Time',
},
default: {},
description: 'Triggers for the workflow',
placeholder: 'Add Cron Time',
options: [
{
name: 'item',
displayName: 'Item',
values: [
{
displayName: 'Mode',
name: 'mode',
type: 'options',
options: [
{
name: 'Every Minute',
value: 'everyMinute',
},
{
name: 'Every Hour',
value: 'everyHour',
},
2019-06-23 03:35:23 -07:00
{
name: 'Every Day',
value: 'everyDay',
2019-06-23 03:35:23 -07:00
},
{
name: 'Every Week',
value: 'everyWeek',
2019-06-23 03:35:23 -07:00
},
{
name: 'Every Month',
value: 'everyMonth',
},
{
name: 'Every X',
value: 'everyX',
2019-06-23 03:35:23 -07:00
},
{
name: 'Custom',
value: 'custom',
},
2019-06-23 03:35:23 -07:00
],
default: 'everyDay',
description: 'How often to trigger.',
},
{
displayName: 'Hour',
name: 'hour',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 23,
},
displayOptions: {
hide: {
mode: [
'custom',
'everyHour',
'everyMinute',
'everyX',
],
},
},
2019-06-23 03:35:23 -07:00
default: 14,
description: 'The hour of the day to trigger (24h format).',
},
{
displayName: 'Minute',
name: 'minute',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 59,
},
displayOptions: {
hide: {
mode: [
'custom',
'everyMinute',
'everyX',
],
},
},
2019-06-23 03:35:23 -07:00
default: 0,
description: 'The minute of the day to trigger.',
},
{
displayName: 'Day of Month',
name: 'dayOfMonth',
type: 'number',
displayOptions: {
show: {
mode: [
'everyMonth',
],
},
},
typeOptions: {
minValue: 1,
maxValue: 31,
},
default: 1,
description: 'The day of the month to trigger.',
},
{
displayName: 'Weekday',
name: 'weekday',
type: 'options',
displayOptions: {
show: {
mode: [
'everyWeek',
],
},
},
options: [
{
name: 'Monday',
value: '1',
},
{
name: 'Tuesday',
value: '2',
},
{
name: 'Wednesday',
value: '3',
},
{
name: 'Thursday',
value: '4',
},
{
name: 'Friday',
value: '5',
},
{
name: 'Saturday',
value: '6',
},
{
name: 'Sunday',
value: '0',
},
],
default: '1',
description: 'The weekday to trigger.',
},
{
displayName: 'Cron Expression',
name: 'cronExpression',
type: 'string',
displayOptions: {
show: {
mode: [
'custom',
],
},
},
default: '* * * * * *',
description: 'Use custom cron expression. Values and ranges as follows:<ul><li>Seconds: 0-59</li><li>Minutes: 0 - 59</li><li>Hours: 0 - 23</li><li>Day of Month: 1 - 31</li><li>Months: 0 - 11 (Jan - Dec)</li><li>Day of Week: 0 - 6 (Sun - Sat)</li></ul>',
},
{
displayName: 'Value',
name: 'value',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 1000,
},
displayOptions: {
show: {
mode: [
'everyX',
],
},
},
default: 2,
description: 'All how many X minutes/hours it should trigger.',
},
{
displayName: 'Unit',
name: 'unit',
type: 'options',
displayOptions: {
show: {
mode: [
'everyX',
],
},
},
options: [
{
name: 'Minutes',
2020-10-22 06:46:03 -07:00
value: 'minutes',
},
{
name: 'Hours',
2020-10-22 06:46:03 -07:00
value: 'hours',
},
],
default: 'hours',
description: 'If it should trigger all X minutes or hours.',
},
2020-10-22 06:46:03 -07:00
],
2019-06-23 03:35:23 -07:00
},
],
2020-10-22 06:46:03 -07:00
},
],
2019-06-23 03:35:23 -07:00
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const triggerTimes = this.getNodeParameter('triggerTimes') as unknown as {
item: TriggerTime[];
};
// Define the order the cron-time-parameter appear
const parameterOrder = [
'second', // 0 - 59
'minute', // 0 - 59
'hour', // 0 - 23
'dayOfMonth', // 1 - 31
'month', // 0 - 11(Jan - Dec)
'weekday', // 0 - 6(Sun - Sat)
];
// Get all the trigger times
const cronTimes: string[] = [];
let cronTime: string[];
let parameterName: string;
if (triggerTimes.item !== undefined) {
for (const item of triggerTimes.item) {
cronTime = [];
if (item.mode === 'custom') {
cronTimes.push(item.cronExpression as string);
continue;
}
2019-11-12 06:09:42 -08:00
if (item.mode === 'everyMinute') {
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} * * * * *`);
2019-11-12 06:09:42 -08:00
continue;
}
if (item.mode === 'everyX') {
if (item.unit === 'minutes') {
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} */${item.value} * * * *`);
} else if (item.unit === 'hours') {
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} 0 */${item.value} * * *`);
}
continue;
}
2019-06-23 03:35:23 -07:00
for (parameterName of parameterOrder) {
if (item[parameterName] !== undefined) {
// Value is set so use it
cronTime.push(item[parameterName] as string);
} else if (parameterName === 'second') {
// For seconds we use by default a random one to make sure to
// balance the load a little bit over time
cronTime.push(Math.floor(Math.random() * 60).toString());
} else {
// For all others set "any"
cronTime.push('*');
}
}
cronTimes.push(cronTime.join(' '));
}
}
// The trigger function to execute when the cron-time got reached
// or when manually triggered
const executeTrigger = () => {
this.emit([this.helpers.returnJsonArray([{}])]);
};
const timezone = this.getTimezone();
// Start the cron-jobs
const cronJobs: CronJob[] = [];
for (const cronTime of cronTimes) {
cronJobs.push(new CronJob(cronTime, executeTrigger, undefined, true, timezone));
}
// Stop the cron-jobs
async function closeFunction() {
for (const cronJob of cronJobs) {
cronJob.stop();
}
}
async function manualTriggerFunction() {
executeTrigger();
}
return {
closeFunction,
manualTriggerFunction,
};
}
}