2019-06-23 03:35:23 -07:00
|
|
|
import { ITriggerFunctions } from 'n8n-core';
|
|
|
|
import {
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
ITriggerResponse,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeOperationError,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
|
|
|
|
export class Interval implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Interval',
|
|
|
|
name: 'interval',
|
|
|
|
icon: 'fa:hourglass',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Triggers the workflow in a given interval',
|
|
|
|
defaults: {
|
|
|
|
name: 'Interval',
|
|
|
|
color: '#00FF00',
|
|
|
|
},
|
|
|
|
inputs: [],
|
|
|
|
outputs: ['main'],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Interval',
|
|
|
|
name: 'interval',
|
|
|
|
type: 'number',
|
|
|
|
typeOptions: {
|
|
|
|
minValue: 1,
|
|
|
|
},
|
|
|
|
default: 1,
|
|
|
|
description: 'Interval value.',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Unit',
|
|
|
|
name: 'unit',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Seconds',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'seconds',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Minutes',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'minutes',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Hours',
|
2020-10-22 06:46:03 -07:00
|
|
|
value: 'hours',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'seconds',
|
|
|
|
description: 'Unit of the interval value.',
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
|
|
|
|
const interval = this.getNodeParameter('interval') as number;
|
|
|
|
const unit = this.getNodeParameter('unit') as string;
|
|
|
|
|
|
|
|
if (interval <= 0) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'The interval has to be set to at least 1 or higher!');
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let intervalValue = interval;
|
|
|
|
if (unit === 'minutes') {
|
|
|
|
intervalValue *= 60;
|
|
|
|
}
|
|
|
|
if (unit === 'hours') {
|
|
|
|
intervalValue *= 60 * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
const executeTrigger = () => {
|
|
|
|
this.emit([this.helpers.returnJsonArray([{}])]);
|
|
|
|
};
|
|
|
|
|
2021-08-01 12:45:44 -07:00
|
|
|
intervalValue *= 1000;
|
|
|
|
|
|
|
|
if (intervalValue > Number.MAX_SAFE_INTEGER) {
|
|
|
|
throw new Error('The interval value is too large.');
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:37:20 -07:00
|
|
|
const intervalObj = setInterval(executeTrigger, intervalValue);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
async function closeFunction() {
|
|
|
|
clearInterval(intervalObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function manualTriggerFunction() {
|
|
|
|
executeTrigger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
closeFunction,
|
|
|
|
manualTriggerFunction,
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|