2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable no-continue */
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
2019-12-31 12:19:37 -08:00
|
|
|
import { CronJob } from 'cron';
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
import {
|
2019-12-31 12:19:37 -08:00
|
|
|
IGetExecutePollFunctions,
|
2019-08-08 11:38:25 -07:00
|
|
|
IGetExecuteTriggerFunctions,
|
2019-12-31 12:19:37 -08:00
|
|
|
INode,
|
|
|
|
IPollResponse,
|
2019-06-23 03:35:23 -07:00
|
|
|
ITriggerResponse,
|
|
|
|
IWorkflowExecuteAdditionalData,
|
2021-05-01 20:43:01 -07:00
|
|
|
LoggerProxy as Logger,
|
2019-06-23 03:35:23 -07:00
|
|
|
Workflow,
|
2021-03-23 11:08:47 -07:00
|
|
|
WorkflowActivateMode,
|
|
|
|
WorkflowExecuteMode,
|
2019-06-23 03:35:23 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line import/no-cycle
|
|
|
|
import { ITriggerTime, IWorkflowData } from '.';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
export class ActiveWorkflows {
|
|
|
|
private workflowData: {
|
2019-12-31 12:19:37 -08:00
|
|
|
[key: string]: IWorkflowData;
|
2019-06-23 03:35:23 -07:00
|
|
|
} = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the workflow is active
|
|
|
|
*
|
|
|
|
* @param {string} id The id of the workflow to check
|
|
|
|
* @returns {boolean}
|
|
|
|
* @memberof ActiveWorkflows
|
|
|
|
*/
|
|
|
|
isActive(id: string): boolean {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-prototype-builtins
|
2019-06-23 03:35:23 -07:00
|
|
|
return this.workflowData.hasOwnProperty(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ids of the currently active workflows
|
|
|
|
*
|
|
|
|
* @returns {string[]}
|
|
|
|
* @memberof ActiveWorkflows
|
|
|
|
*/
|
|
|
|
allActiveWorkflows(): string[] {
|
|
|
|
return Object.keys(this.workflowData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Workflow data for the workflow with
|
|
|
|
* the given id if it is currently active
|
|
|
|
*
|
|
|
|
* @param {string} id
|
|
|
|
* @returns {(WorkflowData | undefined)}
|
|
|
|
* @memberof ActiveWorkflows
|
|
|
|
*/
|
2019-12-31 12:19:37 -08:00
|
|
|
get(id: string): IWorkflowData | undefined {
|
2019-06-23 03:35:23 -07:00
|
|
|
return this.workflowData[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a workflow active
|
|
|
|
*
|
|
|
|
* @param {string} id The id of the workflow to activate
|
|
|
|
* @param {Workflow} workflow The workflow to activate
|
|
|
|
* @param {IWorkflowExecuteAdditionalData} additionalData The additional data which is needed to run workflows
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @memberof ActiveWorkflows
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
async add(
|
|
|
|
id: string,
|
|
|
|
workflow: Workflow,
|
|
|
|
additionalData: IWorkflowExecuteAdditionalData,
|
|
|
|
mode: WorkflowExecuteMode,
|
|
|
|
activation: WorkflowActivateMode,
|
|
|
|
getTriggerFunctions: IGetExecuteTriggerFunctions,
|
|
|
|
getPollFunctions: IGetExecutePollFunctions,
|
|
|
|
): Promise<void> {
|
2020-01-22 15:06:43 -08:00
|
|
|
this.workflowData[id] = {};
|
2019-06-23 03:35:23 -07:00
|
|
|
const triggerNodes = workflow.getTriggerNodes();
|
|
|
|
|
|
|
|
let triggerResponse: ITriggerResponse | undefined;
|
2020-01-10 11:38:55 -08:00
|
|
|
this.workflowData[id].triggerResponses = [];
|
2019-06-23 03:35:23 -07:00
|
|
|
for (const triggerNode of triggerNodes) {
|
2021-08-29 11:58:11 -07:00
|
|
|
triggerResponse = await workflow.runTrigger(
|
|
|
|
triggerNode,
|
|
|
|
getTriggerFunctions,
|
|
|
|
additionalData,
|
|
|
|
mode,
|
|
|
|
activation,
|
|
|
|
);
|
2019-06-23 03:35:23 -07:00
|
|
|
if (triggerResponse !== undefined) {
|
|
|
|
// If a response was given save it
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2020-01-10 11:38:55 -08:00
|
|
|
this.workflowData[id].triggerResponses!.push(triggerResponse);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-31 12:19:37 -08:00
|
|
|
|
|
|
|
const pollNodes = workflow.getPollNodes();
|
2020-01-10 11:38:55 -08:00
|
|
|
if (pollNodes.length) {
|
|
|
|
this.workflowData[id].pollResponses = [];
|
|
|
|
for (const pollNode of pollNodes) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
this.workflowData[id].pollResponses!.push(
|
|
|
|
await this.activatePolling(
|
|
|
|
pollNode,
|
|
|
|
workflow,
|
|
|
|
additionalData,
|
|
|
|
getPollFunctions,
|
|
|
|
mode,
|
|
|
|
activation,
|
|
|
|
),
|
|
|
|
);
|
2020-01-10 11:38:55 -08:00
|
|
|
}
|
2019-12-31 12:19:37 -08:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2019-12-31 12:19:37 -08:00
|
|
|
/**
|
|
|
|
* Activates polling for the given node
|
|
|
|
*
|
|
|
|
* @param {INode} node
|
|
|
|
* @param {Workflow} workflow
|
|
|
|
* @param {IWorkflowExecuteAdditionalData} additionalData
|
|
|
|
* @param {IGetExecutePollFunctions} getPollFunctions
|
|
|
|
* @returns {Promise<IPollResponse>}
|
|
|
|
* @memberof ActiveWorkflows
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
async activatePolling(
|
|
|
|
node: INode,
|
|
|
|
workflow: Workflow,
|
|
|
|
additionalData: IWorkflowExecuteAdditionalData,
|
|
|
|
getPollFunctions: IGetExecutePollFunctions,
|
|
|
|
mode: WorkflowExecuteMode,
|
|
|
|
activation: WorkflowActivateMode,
|
|
|
|
): Promise<IPollResponse> {
|
2021-03-23 11:08:47 -07:00
|
|
|
const pollFunctions = getPollFunctions(workflow, node, additionalData, mode, activation);
|
2019-12-31 12:19:37 -08:00
|
|
|
|
|
|
|
const pollTimes = pollFunctions.getNodeParameter('pollTimes') as unknown as {
|
|
|
|
item: ITriggerTime[];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define the order the cron-time-parameter appear
|
|
|
|
const parameterOrder = [
|
2021-08-29 11:58:11 -07:00
|
|
|
'second', // 0 - 59
|
|
|
|
'minute', // 0 - 59
|
|
|
|
'hour', // 0 - 23
|
2019-12-31 12:19:37 -08:00
|
|
|
'dayOfMonth', // 1 - 31
|
2021-08-29 11:58:11 -07:00
|
|
|
'month', // 0 - 11(Jan - Dec)
|
|
|
|
'weekday', // 0 - 6(Sun - Sat)
|
2019-12-31 12:19:37 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
// Get all the trigger times
|
|
|
|
const cronTimes: string[] = [];
|
|
|
|
let cronTime: string[];
|
|
|
|
let parameterName: string;
|
|
|
|
if (pollTimes.item !== undefined) {
|
|
|
|
for (const item of pollTimes.item) {
|
|
|
|
cronTime = [];
|
|
|
|
if (item.mode === 'custom') {
|
2020-11-12 03:42:04 -08:00
|
|
|
cronTimes.push((item.cronExpression as string).trim());
|
2019-12-31 12:19:37 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (item.mode === 'everyMinute') {
|
|
|
|
cronTimes.push(`${Math.floor(Math.random() * 60).toString()} * * * * *`);
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-31 15:41:47 -08:00
|
|
|
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-12-31 12:19:37 -08: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
|
|
|
|
const executeTrigger = async () => {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
|
|
Logger.info(`Polling trigger initiated for workflow "${workflow.name}"`, {
|
|
|
|
workflowName: workflow.name,
|
|
|
|
workflowId: workflow.id,
|
|
|
|
});
|
2019-12-31 12:19:37 -08:00
|
|
|
const pollResponse = await workflow.runPoll(node, pollFunctions);
|
|
|
|
|
|
|
|
if (pollResponse !== null) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
2019-12-31 12:19:37 -08:00
|
|
|
pollFunctions.__emit(pollResponse);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute the trigger directly to be able to know if it works
|
|
|
|
await executeTrigger();
|
|
|
|
|
|
|
|
const timezone = pollFunctions.getTimezone();
|
|
|
|
|
|
|
|
// Start the cron-jobs
|
|
|
|
const cronJobs: CronJob[] = [];
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
2019-12-31 12:19:37 -08:00
|
|
|
for (const cronTime of cronTimes) {
|
2020-11-12 03:42:04 -08:00
|
|
|
const cronTimeParts = cronTime.split(' ');
|
|
|
|
if (cronTimeParts.length > 0 && cronTimeParts[0].includes('*')) {
|
|
|
|
throw new Error('The polling interval is too short. It has to be at least a minute!');
|
|
|
|
}
|
|
|
|
|
2019-12-31 12:19:37 -08:00
|
|
|
cronJobs.push(new CronJob(cronTime, executeTrigger, undefined, true, timezone));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the cron-jobs
|
|
|
|
async function closeFunction() {
|
|
|
|
for (const cronJob of cronJobs) {
|
|
|
|
cronJob.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
closeFunction,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
/**
|
|
|
|
* Makes a workflow inactive
|
|
|
|
*
|
|
|
|
* @param {string} id The id of the workflow to deactivate
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @memberof ActiveWorkflows
|
|
|
|
*/
|
|
|
|
async remove(id: string): Promise<void> {
|
|
|
|
if (!this.isActive(id)) {
|
|
|
|
// Workflow is currently not registered
|
2021-08-29 11:58:11 -07:00
|
|
|
throw new Error(
|
|
|
|
`The workflow with the id "${id}" is currently not active and can so not be removed`,
|
|
|
|
);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const workflowData = this.workflowData[id];
|
|
|
|
|
2020-01-10 11:38:55 -08:00
|
|
|
if (workflowData.triggerResponses) {
|
|
|
|
for (const triggerResponse of workflowData.triggerResponses) {
|
|
|
|
if (triggerResponse.closeFunction) {
|
|
|
|
await triggerResponse.closeFunction();
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2020-01-10 11:38:55 -08:00
|
|
|
if (workflowData.pollResponses) {
|
|
|
|
for (const pollResponse of workflowData.pollResponses) {
|
|
|
|
if (pollResponse.closeFunction) {
|
|
|
|
await pollResponse.closeFunction();
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 12:19:37 -08:00
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
delete this.workflowData[id];
|
|
|
|
}
|
|
|
|
}
|