2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
|
|
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
|
|
/* eslint-disable @typescript-eslint/no-shadow */
|
|
|
|
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
|
|
/* eslint-disable @typescript-eslint/prefer-optional-chain */
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
|
|
/* eslint-disable import/no-cycle */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
|
import { IProcessMessage, WorkflowExecute } from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ExecutionError,
|
2021-11-05 09:45:51 -07:00
|
|
|
IDeferredPromise,
|
|
|
|
IExecuteResponsePromiseData,
|
2021-08-29 11:58:11 -07:00
|
|
|
IRun,
|
|
|
|
LoggerProxy as Logger,
|
|
|
|
Workflow,
|
|
|
|
WorkflowExecuteMode,
|
|
|
|
WorkflowHooks,
|
|
|
|
WorkflowOperationError,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
|
|
import * as PCancelable from 'p-cancelable';
|
|
|
|
import { join as pathJoin } from 'path';
|
|
|
|
import { fork } from 'child_process';
|
|
|
|
|
|
|
|
import * as Bull from 'bull';
|
|
|
|
import * as config from '../config';
|
|
|
|
// eslint-disable-next-line import/no-cycle
|
2019-08-08 11:38:25 -07:00
|
|
|
import {
|
|
|
|
ActiveExecutions,
|
2020-05-14 05:27:19 -07:00
|
|
|
CredentialsOverwrites,
|
|
|
|
CredentialTypes,
|
2021-02-08 23:59:32 -08:00
|
|
|
Db,
|
2020-05-05 15:59:58 -07:00
|
|
|
ExternalHooks,
|
2021-02-08 23:59:32 -08:00
|
|
|
IBullJobData,
|
|
|
|
IBullJobResponse,
|
2020-05-14 05:27:19 -07:00
|
|
|
ICredentialsOverwrite,
|
|
|
|
ICredentialsTypeData,
|
2021-02-08 23:59:32 -08:00
|
|
|
IExecutionFlattedDb,
|
2019-08-08 11:38:25 -07:00
|
|
|
IProcessMessageDataHook,
|
|
|
|
ITransferNodeTypes,
|
|
|
|
IWorkflowExecutionDataProcess,
|
|
|
|
IWorkflowExecutionDataProcessWithExecution,
|
2020-01-17 17:34:31 -08:00
|
|
|
NodeTypes,
|
2019-08-08 11:38:25 -07:00
|
|
|
Push,
|
2021-02-08 23:59:32 -08:00
|
|
|
ResponseHelper,
|
2021-11-05 09:45:51 -07:00
|
|
|
WebhookHelpers,
|
2019-08-08 11:38:25 -07:00
|
|
|
WorkflowExecuteAdditionalData,
|
2019-10-14 22:36:53 -07:00
|
|
|
WorkflowHelpers,
|
2021-08-29 11:58:11 -07:00
|
|
|
} from '.';
|
2021-02-09 14:32:40 -08:00
|
|
|
import * as Queue from './Queue';
|
2021-10-18 20:57:49 -07:00
|
|
|
import { InternalHooksManager } from './InternalHooksManager';
|
2019-08-08 11:38:25 -07:00
|
|
|
|
|
|
|
export class WorkflowRunner {
|
|
|
|
activeExecutions: ActiveExecutions.ActiveExecutions;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2020-05-14 05:27:19 -07:00
|
|
|
credentialsOverwrites: ICredentialsOverwrite;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
push: Push.Push;
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
jobQueue: Bull.Queue;
|
2019-08-08 11:38:25 -07:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.push = Push.getInstance();
|
|
|
|
this.activeExecutions = ActiveExecutions.getInstance();
|
2020-05-14 05:27:19 -07:00
|
|
|
this.credentialsOverwrites = CredentialsOverwrites().getAll();
|
2021-02-08 23:59:32 -08:00
|
|
|
|
|
|
|
const executionsMode = config.get('executions.mode') as string;
|
|
|
|
|
|
|
|
if (executionsMode === 'queue') {
|
2021-02-09 14:32:40 -08:00
|
|
|
this.jobQueue = Queue.getInstance().getBullObjectInstance();
|
2021-02-08 23:59:32 -08:00
|
|
|
}
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The process did send a hook message so execute the appropiate hook
|
|
|
|
*
|
2019-12-19 14:07:55 -08:00
|
|
|
* @param {WorkflowHooks} workflowHooks
|
2019-08-08 11:38:25 -07:00
|
|
|
* @param {IProcessMessageDataHook} hookData
|
|
|
|
* @memberof WorkflowRunner
|
|
|
|
*/
|
2019-12-19 14:07:55 -08:00
|
|
|
processHookMessage(workflowHooks: WorkflowHooks, hookData: IProcessMessageDataHook) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2019-12-19 14:07:55 -08:00
|
|
|
workflowHooks.executeHookFunctions(hookData.hook, hookData.parameters);
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The process did error
|
|
|
|
*
|
2021-04-16 09:33:36 -07:00
|
|
|
* @param {ExecutionError} error
|
2019-08-08 11:38:25 -07:00
|
|
|
* @param {Date} startedAt
|
|
|
|
* @param {WorkflowExecuteMode} executionMode
|
|
|
|
* @param {string} executionId
|
|
|
|
* @memberof WorkflowRunner
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
async processError(
|
|
|
|
error: ExecutionError,
|
|
|
|
startedAt: Date,
|
|
|
|
executionMode: WorkflowExecuteMode,
|
|
|
|
executionId: string,
|
|
|
|
hooks?: WorkflowHooks,
|
|
|
|
) {
|
2019-08-08 11:38:25 -07:00
|
|
|
const fullRunData: IRun = {
|
|
|
|
data: {
|
|
|
|
resultData: {
|
2021-07-10 02:34:41 -07:00
|
|
|
error: {
|
|
|
|
...error,
|
|
|
|
message: error.message,
|
|
|
|
stack: error.stack,
|
|
|
|
},
|
2019-08-08 11:38:25 -07:00
|
|
|
runData: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
finished: false,
|
|
|
|
mode: executionMode,
|
|
|
|
startedAt,
|
|
|
|
stoppedAt: new Date(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove from active execution with empty data. That will
|
|
|
|
// set the execution to failed.
|
|
|
|
this.activeExecutions.remove(executionId, fullRunData);
|
2021-07-10 02:34:41 -07:00
|
|
|
|
|
|
|
if (hooks) {
|
|
|
|
await hooks.executeHookFunctions('workflowExecuteAfter', [fullRunData]);
|
|
|
|
}
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 17:34:31 -08:00
|
|
|
* Run the workflow
|
2019-08-08 11:38:25 -07:00
|
|
|
*
|
|
|
|
* @param {IWorkflowExecutionDataProcess} data
|
2019-10-14 22:36:53 -07:00
|
|
|
* @param {boolean} [loadStaticData] If set will the static data be loaded from
|
|
|
|
* the workflow and added to input data
|
2019-08-08 11:38:25 -07:00
|
|
|
* @returns {Promise<string>}
|
|
|
|
* @memberof WorkflowRunner
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
async run(
|
|
|
|
data: IWorkflowExecutionDataProcess,
|
|
|
|
loadStaticData?: boolean,
|
|
|
|
realtime?: boolean,
|
|
|
|
executionId?: string,
|
2021-11-05 09:45:51 -07:00
|
|
|
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
|
2021-08-29 11:58:11 -07:00
|
|
|
): Promise<string> {
|
2020-01-17 17:49:31 -08:00
|
|
|
const executionsProcess = config.get('executions.process') as string;
|
2021-02-08 23:59:32 -08:00
|
|
|
const executionsMode = config.get('executions.mode') as string;
|
2020-10-20 10:01:40 -07:00
|
|
|
|
2021-02-08 23:59:32 -08:00
|
|
|
if (executionsMode === 'queue' && data.executionMode !== 'manual') {
|
|
|
|
// Do not run "manual" executions in bull because sending events to the
|
|
|
|
// frontend would not be possible
|
2021-11-05 09:45:51 -07:00
|
|
|
executionId = await this.runBull(
|
|
|
|
data,
|
|
|
|
loadStaticData,
|
|
|
|
realtime,
|
|
|
|
executionId,
|
|
|
|
responsePromise,
|
|
|
|
);
|
2021-02-08 23:59:32 -08:00
|
|
|
} else if (executionsProcess === 'main') {
|
2021-11-05 09:45:51 -07:00
|
|
|
executionId = await this.runMainProcess(data, loadStaticData, executionId, responsePromise);
|
2020-10-20 10:01:40 -07:00
|
|
|
} else {
|
2021-11-05 09:45:51 -07:00
|
|
|
executionId = await this.runSubprocess(data, loadStaticData, executionId, responsePromise);
|
2020-10-20 10:01:40 -07:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:57:49 -07:00
|
|
|
const postExecutePromise = this.activeExecutions.getPostExecutePromise(executionId);
|
|
|
|
|
2020-11-13 14:31:27 -08:00
|
|
|
const externalHooks = ExternalHooks();
|
2021-10-18 20:57:49 -07:00
|
|
|
postExecutePromise
|
|
|
|
.then(async (executionData) => {
|
|
|
|
void InternalHooksManager.getInstance().onWorkflowPostExecute(
|
|
|
|
data.workflowData,
|
|
|
|
executionData,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('There was a problem running internal hook "onWorkflowPostExecute"', error);
|
|
|
|
});
|
|
|
|
|
2020-10-20 10:01:40 -07:00
|
|
|
if (externalHooks.exists('workflow.postExecute')) {
|
2021-10-18 20:57:49 -07:00
|
|
|
postExecutePromise
|
2020-10-20 10:01:40 -07:00
|
|
|
.then(async (executionData) => {
|
|
|
|
await externalHooks.run('workflow.postExecute', [executionData, data.workflowData]);
|
|
|
|
})
|
2021-08-29 11:58:11 -07:00
|
|
|
.catch((error) => {
|
2020-10-20 10:01:40 -07:00
|
|
|
console.error('There was a problem running hook "workflow.postExecute"', error);
|
|
|
|
});
|
2020-01-17 17:34:31 -08:00
|
|
|
}
|
|
|
|
|
2020-10-20 10:01:40 -07:00
|
|
|
return executionId;
|
2020-01-17 17:34:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the workflow in current process
|
|
|
|
*
|
|
|
|
* @param {IWorkflowExecutionDataProcess} data
|
|
|
|
* @param {boolean} [loadStaticData] If set will the static data be loaded from
|
|
|
|
* the workflow and added to input data
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
* @memberof WorkflowRunner
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
async runMainProcess(
|
|
|
|
data: IWorkflowExecutionDataProcess,
|
|
|
|
loadStaticData?: boolean,
|
|
|
|
restartExecutionId?: string,
|
2021-11-05 09:45:51 -07:00
|
|
|
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
|
2021-08-29 11:58:11 -07:00
|
|
|
): Promise<string> {
|
2020-01-17 17:34:31 -08:00
|
|
|
if (loadStaticData === true && data.workflowData.id) {
|
2021-08-29 11:58:11 -07:00
|
|
|
data.workflowData.staticData = await WorkflowHelpers.getStaticDataById(
|
|
|
|
data.workflowData.id as string,
|
|
|
|
);
|
2020-01-17 17:34:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const nodeTypes = NodeTypes();
|
|
|
|
|
2021-04-17 07:44:07 -07:00
|
|
|
// Soft timeout to stop workflow execution after current running node
|
|
|
|
// Changes were made by adding the `workflowTimeout` to the `additionalData`
|
|
|
|
// So that the timeout will also work for executions with nested workflows.
|
|
|
|
let executionTimeout: NodeJS.Timeout;
|
|
|
|
let workflowTimeout = config.get('executions.timeout') as number; // initialize with default
|
|
|
|
if (data.workflowData.settings && data.workflowData.settings.executionTimeout) {
|
2021-08-29 11:58:11 -07:00
|
|
|
workflowTimeout = data.workflowData.settings.executionTimeout as number; // preference on workflow setting
|
2021-04-17 07:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (workflowTimeout > 0) {
|
|
|
|
workflowTimeout = Math.min(workflowTimeout, config.get('executions.maxTimeout') as number);
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
const workflow = new Workflow({
|
|
|
|
id: data.workflowData.id as string | undefined,
|
|
|
|
name: data.workflowData.name,
|
|
|
|
nodes: data.workflowData.nodes,
|
|
|
|
connections: data.workflowData.connections,
|
|
|
|
active: data.workflowData.active,
|
|
|
|
nodeTypes,
|
|
|
|
staticData: data.workflowData.staticData,
|
|
|
|
});
|
|
|
|
const additionalData = await WorkflowExecuteAdditionalData.getBase(
|
|
|
|
undefined,
|
|
|
|
workflowTimeout <= 0 ? undefined : Date.now() + workflowTimeout * 1000,
|
|
|
|
);
|
2020-01-17 17:34:31 -08:00
|
|
|
|
|
|
|
// Register the active execution
|
2021-08-29 11:58:11 -07:00
|
|
|
const executionId = await this.activeExecutions.add(data, undefined, restartExecutionId);
|
2021-08-21 05:11:32 -07:00
|
|
|
additionalData.executionId = executionId;
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
Logger.verbose(
|
|
|
|
`Execution for workflow ${data.workflowData.name} was assigned id ${executionId}`,
|
|
|
|
{ executionId },
|
|
|
|
);
|
2021-07-10 02:34:41 -07:00
|
|
|
let workflowExecution: PCancelable<IRun>;
|
2020-01-17 17:34:31 -08:00
|
|
|
|
2021-07-10 02:34:41 -07:00
|
|
|
try {
|
2021-08-29 11:58:11 -07:00
|
|
|
Logger.verbose(
|
|
|
|
`Execution for workflow ${data.workflowData.name} was assigned id ${executionId}`,
|
|
|
|
{ executionId },
|
|
|
|
);
|
|
|
|
additionalData.hooks = WorkflowExecuteAdditionalData.getWorkflowHooksMain(
|
|
|
|
data,
|
|
|
|
executionId,
|
|
|
|
true,
|
|
|
|
);
|
2021-11-05 09:45:51 -07:00
|
|
|
|
|
|
|
additionalData.hooks.hookFunctions.sendResponse = [
|
|
|
|
async (response: IExecuteResponsePromiseData): Promise<void> => {
|
|
|
|
if (responsePromise) {
|
|
|
|
responsePromise.resolve(response);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
additionalData.sendMessageToUI = WorkflowExecuteAdditionalData.sendMessageToUI.bind({
|
|
|
|
sessionId: data.sessionId,
|
|
|
|
});
|
2021-05-29 11:41:25 -07:00
|
|
|
|
2021-07-10 02:34:41 -07:00
|
|
|
if (data.executionData !== undefined) {
|
2021-08-29 11:58:11 -07:00
|
|
|
Logger.debug(`Execution ID ${executionId} had Execution data. Running with payload.`, {
|
|
|
|
executionId,
|
|
|
|
});
|
|
|
|
const workflowExecute = new WorkflowExecute(
|
|
|
|
additionalData,
|
|
|
|
data.executionMode,
|
|
|
|
data.executionData,
|
|
|
|
);
|
2021-07-10 02:34:41 -07:00
|
|
|
workflowExecution = workflowExecute.processRunExecutionData(workflow);
|
2021-08-29 11:58:11 -07:00
|
|
|
} else if (
|
|
|
|
data.runData === undefined ||
|
|
|
|
data.startNodes === undefined ||
|
|
|
|
data.startNodes.length === 0 ||
|
|
|
|
data.destinationNode === undefined
|
|
|
|
) {
|
|
|
|
Logger.debug(`Execution ID ${executionId} will run executing all nodes.`, { executionId });
|
2021-07-10 02:34:41 -07:00
|
|
|
// Execute all nodes
|
|
|
|
|
|
|
|
// Can execute without webhook so go on
|
|
|
|
const workflowExecute = new WorkflowExecute(additionalData, data.executionMode);
|
|
|
|
workflowExecution = workflowExecute.run(workflow, undefined, data.destinationNode);
|
|
|
|
} else {
|
2021-08-29 11:58:11 -07:00
|
|
|
Logger.debug(`Execution ID ${executionId} is a partial execution.`, { executionId });
|
2021-07-10 02:34:41 -07:00
|
|
|
// Execute only the nodes between start and destination nodes
|
|
|
|
const workflowExecute = new WorkflowExecute(additionalData, data.executionMode);
|
2021-08-29 11:58:11 -07:00
|
|
|
workflowExecution = workflowExecute.runPartialWorkflow(
|
|
|
|
workflow,
|
|
|
|
data.runData,
|
|
|
|
data.startNodes,
|
|
|
|
data.destinationNode,
|
|
|
|
);
|
2021-07-10 02:34:41 -07:00
|
|
|
}
|
|
|
|
|
2021-07-14 07:34:54 -07:00
|
|
|
this.activeExecutions.attachWorkflowExecution(executionId, workflowExecution);
|
|
|
|
|
|
|
|
if (workflowTimeout > 0) {
|
2021-08-29 11:58:11 -07:00
|
|
|
const timeout =
|
|
|
|
Math.min(workflowTimeout, config.get('executions.maxTimeout') as number) * 1000; // as seconds
|
2021-07-14 07:34:54 -07:00
|
|
|
executionTimeout = setTimeout(() => {
|
|
|
|
this.activeExecutions.stopExecution(executionId, 'timeout');
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
workflowExecution
|
|
|
|
.then((fullRunData) => {
|
|
|
|
clearTimeout(executionTimeout);
|
|
|
|
if (workflowExecution.isCanceled) {
|
|
|
|
fullRunData.finished = false;
|
|
|
|
}
|
|
|
|
this.activeExecutions.remove(executionId, fullRunData);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.processError(
|
|
|
|
error,
|
|
|
|
new Date(),
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
additionalData.hooks,
|
|
|
|
);
|
|
|
|
});
|
2021-07-10 02:34:41 -07:00
|
|
|
} catch (error) {
|
2021-08-29 11:58:11 -07:00
|
|
|
await this.processError(
|
|
|
|
error,
|
|
|
|
new Date(),
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
additionalData.hooks,
|
|
|
|
);
|
2021-07-10 02:34:41 -07:00
|
|
|
|
|
|
|
throw error;
|
2020-01-17 17:34:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return executionId;
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
async runBull(
|
|
|
|
data: IWorkflowExecutionDataProcess,
|
|
|
|
loadStaticData?: boolean,
|
|
|
|
realtime?: boolean,
|
|
|
|
restartExecutionId?: string,
|
2021-11-05 09:45:51 -07:00
|
|
|
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
|
2021-08-29 11:58:11 -07:00
|
|
|
): Promise<string> {
|
2021-02-08 23:59:32 -08:00
|
|
|
// TODO: If "loadStaticData" is set to true it has to load data new on worker
|
|
|
|
|
|
|
|
// Register the active execution
|
2021-08-21 05:11:32 -07:00
|
|
|
const executionId = await this.activeExecutions.add(data, undefined, restartExecutionId);
|
2021-11-05 09:45:51 -07:00
|
|
|
if (responsePromise) {
|
|
|
|
this.activeExecutions.attachResponsePromise(executionId, responsePromise);
|
|
|
|
}
|
2021-02-08 23:59:32 -08:00
|
|
|
|
|
|
|
const jobData: IBullJobData = {
|
|
|
|
executionId,
|
|
|
|
loadStaticData: !!loadStaticData,
|
|
|
|
};
|
|
|
|
|
|
|
|
let priority = 100;
|
|
|
|
if (realtime === true) {
|
|
|
|
// Jobs which require a direct response get a higher priority
|
|
|
|
priority = 50;
|
|
|
|
}
|
|
|
|
// TODO: For realtime jobs should probably also not do retry or not retry if they are older than x seconds.
|
|
|
|
// Check if they get retried by default and how often.
|
|
|
|
const jobOptions = {
|
|
|
|
priority,
|
|
|
|
removeOnComplete: true,
|
|
|
|
removeOnFail: true,
|
|
|
|
};
|
2021-07-10 02:34:41 -07:00
|
|
|
let job: Bull.Job;
|
2021-07-11 09:05:44 -07:00
|
|
|
let hooks: WorkflowHooks;
|
2021-07-10 02:34:41 -07:00
|
|
|
try {
|
|
|
|
job = await this.jobQueue.add(jobData, jobOptions);
|
2021-07-11 09:05:44 -07:00
|
|
|
|
2021-09-02 09:58:13 -07:00
|
|
|
console.log(`Started with job ID: ${job.id.toString()} (Execution ID: ${executionId})`);
|
2021-07-11 09:05:44 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
hooks = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerMain(
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
data.workflowData,
|
|
|
|
{ retryOf: data.retryOf ? data.retryOf.toString() : undefined },
|
|
|
|
);
|
2021-07-11 09:05:44 -07:00
|
|
|
|
|
|
|
// Normally also workflow should be supplied here but as it only used for sending
|
|
|
|
// data to editor-UI is not needed.
|
|
|
|
hooks.executeHookFunctions('workflowExecuteBefore', []);
|
2021-07-10 02:34:41 -07:00
|
|
|
} catch (error) {
|
2021-07-12 01:12:45 -07:00
|
|
|
// We use "getWorkflowHooksWorkerExecuter" as "getWorkflowHooksWorkerMain" does not contain the
|
|
|
|
// "workflowExecuteAfter" which we require.
|
2021-08-29 11:58:11 -07:00
|
|
|
const hooks = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerExecuter(
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
data.workflowData,
|
|
|
|
{ retryOf: data.retryOf ? data.retryOf.toString() : undefined },
|
|
|
|
);
|
2021-07-10 02:34:41 -07:00
|
|
|
await this.processError(error, new Date(), data.executionMode, executionId, hooks);
|
2021-07-12 01:12:45 -07:00
|
|
|
throw error;
|
2021-07-10 02:34:41 -07:00
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
const workflowExecution: PCancelable<IRun> = new PCancelable(
|
|
|
|
async (resolve, reject, onCancel) => {
|
|
|
|
onCancel.shouldReject = false;
|
|
|
|
onCancel(async () => {
|
|
|
|
await Queue.getInstance().stopJob(job);
|
|
|
|
|
|
|
|
// We use "getWorkflowHooksWorkerExecuter" as "getWorkflowHooksWorkerMain" does not contain the
|
|
|
|
// "workflowExecuteAfter" which we require.
|
|
|
|
const hooksWorker = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerExecuter(
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
data.workflowData,
|
|
|
|
{ retryOf: data.retryOf ? data.retryOf.toString() : undefined },
|
|
|
|
);
|
|
|
|
|
|
|
|
const error = new WorkflowOperationError('Workflow-Execution has been canceled!');
|
|
|
|
await this.processError(error, new Date(), data.executionMode, executionId, hooksWorker);
|
|
|
|
|
|
|
|
reject(error);
|
2021-02-08 23:59:32 -08:00
|
|
|
});
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
const jobData: Promise<IBullJobResponse> = job.finished();
|
|
|
|
|
|
|
|
const queueRecoveryInterval = config.get('queue.bull.queueRecoveryInterval') as number;
|
|
|
|
|
|
|
|
const racingPromises: Array<Promise<IBullJobResponse | object>> = [jobData];
|
|
|
|
|
|
|
|
let clearWatchdogInterval;
|
|
|
|
if (queueRecoveryInterval > 0) {
|
|
|
|
/** ***********************************************
|
|
|
|
* Long explanation about what this solves: *
|
|
|
|
* This only happens in a very specific scenario *
|
|
|
|
* when Redis crashes and recovers shortly *
|
|
|
|
* but during this time, some execution(s) *
|
|
|
|
* finished. The end result is that the main *
|
|
|
|
* process will wait indefinitively and never *
|
|
|
|
* get a response. This adds an active polling to*
|
|
|
|
* the queue that allows us to identify that the *
|
|
|
|
* execution finished and get information from *
|
|
|
|
* the database. *
|
|
|
|
************************************************ */
|
|
|
|
let watchDogInterval: NodeJS.Timeout | undefined;
|
|
|
|
|
|
|
|
const watchDog: Promise<object> = new Promise((res) => {
|
|
|
|
watchDogInterval = setInterval(async () => {
|
|
|
|
const currentJob = await this.jobQueue.getJob(job.id);
|
|
|
|
// When null means job is finished (not found in queue)
|
|
|
|
if (currentJob === null) {
|
|
|
|
// Mimic worker's success message
|
|
|
|
res({ success: true });
|
|
|
|
}
|
|
|
|
}, queueRecoveryInterval * 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
racingPromises.push(watchDog);
|
|
|
|
|
|
|
|
clearWatchdogInterval = () => {
|
|
|
|
if (watchDogInterval) {
|
|
|
|
clearInterval(watchDogInterval);
|
|
|
|
watchDogInterval = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-02-13 11:40:27 -08:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
try {
|
|
|
|
await Promise.race(racingPromises);
|
|
|
|
if (clearWatchdogInterval !== undefined) {
|
|
|
|
clearWatchdogInterval();
|
2021-02-08 23:59:32 -08:00
|
|
|
}
|
2021-08-29 11:58:11 -07:00
|
|
|
} catch (error) {
|
|
|
|
// We use "getWorkflowHooksWorkerExecuter" as "getWorkflowHooksWorkerMain" does not contain the
|
|
|
|
// "workflowExecuteAfter" which we require.
|
|
|
|
const hooks = WorkflowExecuteAdditionalData.getWorkflowHooksWorkerExecuter(
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
data.workflowData,
|
|
|
|
{ retryOf: data.retryOf ? data.retryOf.toString() : undefined },
|
|
|
|
);
|
|
|
|
Logger.error(`Problem with execution ${executionId}: ${error.message}. Aborting.`);
|
|
|
|
if (clearWatchdogInterval !== undefined) {
|
|
|
|
clearWatchdogInterval();
|
|
|
|
}
|
|
|
|
await this.processError(error, new Date(), data.executionMode, executionId, hooks);
|
2021-02-08 23:59:32 -08:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
reject(error);
|
2021-07-10 02:34:41 -07:00
|
|
|
}
|
2021-04-17 07:44:07 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
const executionDb = (await Db.collections.Execution!.findOne(
|
|
|
|
executionId,
|
|
|
|
)) as IExecutionFlattedDb;
|
|
|
|
const fullExecutionData = ResponseHelper.unflattenExecutionData(executionDb);
|
|
|
|
const runData = {
|
|
|
|
data: fullExecutionData.data,
|
|
|
|
finished: fullExecutionData.finished,
|
|
|
|
mode: fullExecutionData.mode,
|
|
|
|
startedAt: fullExecutionData.startedAt,
|
|
|
|
stoppedAt: fullExecutionData.stoppedAt,
|
|
|
|
} as IRun;
|
|
|
|
|
|
|
|
this.activeExecutions.remove(executionId, runData);
|
|
|
|
// Normally also static data should be supplied here but as it only used for sending
|
|
|
|
// data to editor-UI is not needed.
|
|
|
|
hooks.executeHookFunctions('workflowExecuteAfter', [runData]);
|
|
|
|
try {
|
|
|
|
// Check if this execution data has to be removed from database
|
|
|
|
// based on workflow settings.
|
|
|
|
let saveDataErrorExecution = config.get('executions.saveDataOnError') as string;
|
|
|
|
let saveDataSuccessExecution = config.get('executions.saveDataOnSuccess') as string;
|
|
|
|
if (data.workflowData.settings !== undefined) {
|
|
|
|
saveDataErrorExecution =
|
|
|
|
(data.workflowData.settings.saveDataErrorExecution as string) ||
|
|
|
|
saveDataErrorExecution;
|
|
|
|
saveDataSuccessExecution =
|
|
|
|
(data.workflowData.settings.saveDataSuccessExecution as string) ||
|
|
|
|
saveDataSuccessExecution;
|
|
|
|
}
|
2021-03-10 06:51:18 -08:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
const workflowDidSucceed = !runData.data.resultData.error;
|
|
|
|
if (
|
|
|
|
(workflowDidSucceed && saveDataSuccessExecution === 'none') ||
|
|
|
|
(!workflowDidSucceed && saveDataErrorExecution === 'none')
|
|
|
|
) {
|
|
|
|
await Db.collections.Execution!.delete(executionId);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line id-denylist
|
|
|
|
} catch (err) {
|
|
|
|
// We don't want errors here to crash n8n. Just log and proceed.
|
|
|
|
console.log('Error removing saved execution from database. More details: ', err);
|
2021-03-10 06:51:18 -08:00
|
|
|
}
|
2021-04-17 07:44:07 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
resolve(runData);
|
|
|
|
},
|
|
|
|
);
|
2021-02-08 23:59:32 -08:00
|
|
|
|
|
|
|
this.activeExecutions.attachWorkflowExecution(executionId, workflowExecution);
|
|
|
|
return executionId;
|
|
|
|
}
|
|
|
|
|
2020-01-17 17:34:31 -08:00
|
|
|
/**
|
|
|
|
* Run the workflow
|
|
|
|
*
|
|
|
|
* @param {IWorkflowExecutionDataProcess} data
|
|
|
|
* @param {boolean} [loadStaticData] If set will the static data be loaded from
|
|
|
|
* the workflow and added to input data
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
* @memberof WorkflowRunner
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
async runSubprocess(
|
|
|
|
data: IWorkflowExecutionDataProcess,
|
|
|
|
loadStaticData?: boolean,
|
|
|
|
restartExecutionId?: string,
|
2021-11-05 09:45:51 -07:00
|
|
|
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
|
2021-08-29 11:58:11 -07:00
|
|
|
): Promise<string> {
|
2021-04-17 07:44:07 -07:00
|
|
|
let startedAt = new Date();
|
2019-08-09 04:12:00 -07:00
|
|
|
const subprocess = fork(pathJoin(__dirname, 'WorkflowRunnerProcess.js'));
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2019-10-14 22:36:53 -07:00
|
|
|
if (loadStaticData === true && data.workflowData.id) {
|
2021-08-29 11:58:11 -07:00
|
|
|
data.workflowData.staticData = await WorkflowHelpers.getStaticDataById(
|
|
|
|
data.workflowData.id as string,
|
|
|
|
);
|
2019-10-14 22:36:53 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
// Register the active execution
|
2021-08-21 05:11:32 -07:00
|
|
|
const executionId = await this.activeExecutions.add(data, subprocess, restartExecutionId);
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2021-08-26 04:45:15 -07:00
|
|
|
// Check if workflow contains a "executeWorkflow" Node as in this
|
|
|
|
// case we can not know which nodeTypes and credentialTypes will
|
|
|
|
// be needed and so have to load all of them in the workflowRunnerProcess
|
|
|
|
let loadAllNodeTypes = false;
|
|
|
|
for (const node of data.workflowData.nodes) {
|
|
|
|
if (node.type === 'n8n-nodes-base.executeWorkflow') {
|
|
|
|
loadAllNodeTypes = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let nodeTypeData: ITransferNodeTypes;
|
|
|
|
let credentialTypeData: ICredentialsTypeData;
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line prefer-destructuring
|
2021-08-26 04:45:15 -07:00
|
|
|
let credentialsOverwrites = this.credentialsOverwrites;
|
2021-08-29 11:58:11 -07:00
|
|
|
if (loadAllNodeTypes) {
|
2021-08-26 04:45:15 -07:00
|
|
|
// Supply all nodeTypes and credentialTypes
|
|
|
|
nodeTypeData = WorkflowHelpers.getAllNodeTypeData();
|
|
|
|
const credentialTypes = CredentialTypes();
|
|
|
|
credentialTypeData = credentialTypes.credentialTypes;
|
|
|
|
} else {
|
|
|
|
// Supply only nodeTypes, credentialTypes and overwrites that the workflow needs
|
|
|
|
nodeTypeData = WorkflowHelpers.getNodeTypeData(data.workflowData.nodes);
|
|
|
|
credentialTypeData = WorkflowHelpers.getCredentialsDataByNodes(data.workflowData.nodes);
|
|
|
|
|
|
|
|
credentialsOverwrites = {};
|
|
|
|
for (const credentialName of Object.keys(credentialTypeData)) {
|
|
|
|
if (this.credentialsOverwrites[credentialName] !== undefined) {
|
|
|
|
credentialsOverwrites[credentialName] = this.credentialsOverwrites[credentialName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 11:38:25 -07:00
|
|
|
|
|
|
|
(data as unknown as IWorkflowExecutionDataProcessWithExecution).executionId = executionId;
|
|
|
|
(data as unknown as IWorkflowExecutionDataProcessWithExecution).nodeTypeData = nodeTypeData;
|
2021-08-29 11:58:11 -07:00
|
|
|
(data as unknown as IWorkflowExecutionDataProcessWithExecution).credentialsOverwrite =
|
|
|
|
this.credentialsOverwrites;
|
|
|
|
(data as unknown as IWorkflowExecutionDataProcessWithExecution).credentialsTypeData =
|
|
|
|
credentialTypeData;
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2019-12-19 14:07:55 -08:00
|
|
|
const workflowHooks = WorkflowExecuteAdditionalData.getWorkflowHooksMain(data, executionId);
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2021-07-10 02:34:41 -07:00
|
|
|
try {
|
|
|
|
// Send all data to subprocess it needs to run the workflow
|
|
|
|
subprocess.send({ type: 'startWorkflow', data } as IProcessMessage);
|
|
|
|
} catch (error) {
|
|
|
|
await this.processError(error, new Date(), data.executionMode, executionId, workflowHooks);
|
|
|
|
return executionId;
|
|
|
|
}
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2020-07-29 05:12:54 -07:00
|
|
|
// Start timeout for the execution
|
|
|
|
let executionTimeout: NodeJS.Timeout;
|
2021-04-17 07:44:07 -07:00
|
|
|
let workflowTimeout = config.get('executions.timeout') as number; // initialize with default
|
2020-07-29 05:12:54 -07:00
|
|
|
if (data.workflowData.settings && data.workflowData.settings.executionTimeout) {
|
2021-08-29 11:58:11 -07:00
|
|
|
workflowTimeout = data.workflowData.settings.executionTimeout as number; // preference on workflow setting
|
2020-07-29 05:12:54 -07:00
|
|
|
}
|
|
|
|
|
2021-04-17 07:44:07 -07:00
|
|
|
const processTimeoutFunction = (timeout: number) => {
|
|
|
|
this.activeExecutions.stopExecution(executionId, 'timeout');
|
|
|
|
executionTimeout = setTimeout(() => subprocess.kill(), Math.max(timeout * 0.2, 5000)); // minimum 5 seconds
|
2021-04-17 08:23:48 -07:00
|
|
|
};
|
2020-07-29 05:12:54 -07:00
|
|
|
|
2021-04-17 07:44:07 -07:00
|
|
|
if (workflowTimeout > 0) {
|
2021-08-29 11:58:11 -07:00
|
|
|
workflowTimeout =
|
|
|
|
Math.min(workflowTimeout, config.get('executions.maxTimeout') as number) * 1000; // as seconds
|
2021-04-17 07:44:07 -07:00
|
|
|
// Start timeout already now but give process at least 5 seconds to start.
|
|
|
|
// Without it could would it be possible that the workflow executions times out before it even got started if
|
|
|
|
// the timeout time is very short as the process start time can be quite long.
|
2021-08-29 11:58:11 -07:00
|
|
|
executionTimeout = setTimeout(
|
|
|
|
processTimeoutFunction,
|
|
|
|
Math.max(5000, workflowTimeout),
|
|
|
|
workflowTimeout,
|
|
|
|
);
|
2020-07-29 05:12:54 -07:00
|
|
|
}
|
|
|
|
|
2021-04-17 07:44:07 -07:00
|
|
|
// Create a list of child spawned executions
|
|
|
|
// If after the child process exits we have
|
|
|
|
// outstanding executions, we remove them
|
|
|
|
const childExecutionIds: string[] = [];
|
2020-07-29 05:12:54 -07:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
// Listen to data from the subprocess
|
2021-02-20 04:51:06 -08:00
|
|
|
subprocess.on('message', async (message: IProcessMessage) => {
|
2021-08-29 11:58:11 -07:00
|
|
|
Logger.debug(
|
|
|
|
`Received child process message of type ${message.type} for execution ID ${executionId}.`,
|
|
|
|
{ executionId },
|
|
|
|
);
|
2021-04-17 07:44:07 -07:00
|
|
|
if (message.type === 'start') {
|
|
|
|
// Now that the execution actually started set the timeout again so that does not time out to early.
|
|
|
|
startedAt = new Date();
|
|
|
|
if (workflowTimeout > 0) {
|
|
|
|
clearTimeout(executionTimeout);
|
|
|
|
executionTimeout = setTimeout(processTimeoutFunction, workflowTimeout, workflowTimeout);
|
|
|
|
}
|
|
|
|
} else if (message.type === 'end') {
|
2020-07-29 05:12:54 -07:00
|
|
|
clearTimeout(executionTimeout);
|
2021-08-29 11:58:11 -07:00
|
|
|
this.activeExecutions.remove(executionId, message.data.runData);
|
2021-11-05 09:45:51 -07:00
|
|
|
} else if (message.type === 'sendResponse') {
|
|
|
|
if (responsePromise) {
|
|
|
|
responsePromise.resolve(WebhookHelpers.decodeWebhookResponse(message.data.response));
|
|
|
|
}
|
2021-05-29 11:41:25 -07:00
|
|
|
} else if (message.type === 'sendMessageToUI') {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
WorkflowExecuteAdditionalData.sendMessageToUI.bind({ sessionId: data.sessionId })(
|
|
|
|
message.data.source,
|
|
|
|
message.data.message,
|
|
|
|
);
|
2020-07-29 05:12:54 -07:00
|
|
|
} else if (message.type === 'processError') {
|
|
|
|
clearTimeout(executionTimeout);
|
2021-04-16 09:33:36 -07:00
|
|
|
const executionError = message.data.executionError as ExecutionError;
|
2021-08-29 11:58:11 -07:00
|
|
|
await this.processError(
|
|
|
|
executionError,
|
|
|
|
startedAt,
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
workflowHooks,
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
} else if (message.type === 'processHook') {
|
2019-12-19 14:07:55 -08:00
|
|
|
this.processHookMessage(workflowHooks, message.data as IProcessMessageDataHook);
|
2020-07-29 05:12:54 -07:00
|
|
|
} else if (message.type === 'timeout') {
|
|
|
|
// Execution timed out and its process has been terminated
|
2021-04-16 09:33:36 -07:00
|
|
|
const timeoutError = new WorkflowOperationError('Workflow execution timed out!');
|
2020-07-29 05:12:54 -07:00
|
|
|
|
2021-07-10 02:34:41 -07:00
|
|
|
// No need to add hook here as the subprocess takes care of calling the hooks
|
2020-07-29 05:12:54 -07:00
|
|
|
this.processError(timeoutError, startedAt, data.executionMode, executionId);
|
2021-02-20 04:51:06 -08:00
|
|
|
} else if (message.type === 'startExecution') {
|
|
|
|
const executionId = await this.activeExecutions.add(message.data.runData);
|
2021-04-17 07:44:07 -07:00
|
|
|
childExecutionIds.push(executionId);
|
2021-08-29 11:58:11 -07:00
|
|
|
subprocess.send({ type: 'executionId', data: { executionId } } as IProcessMessage);
|
2021-02-20 04:51:06 -08:00
|
|
|
} else if (message.type === 'finishExecution') {
|
2021-04-17 07:44:07 -07:00
|
|
|
const executionIdIndex = childExecutionIds.indexOf(message.data.executionId);
|
|
|
|
if (executionIdIndex !== -1) {
|
|
|
|
childExecutionIds.splice(executionIdIndex, 1);
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/await-thenable
|
2021-02-20 04:51:06 -08:00
|
|
|
await this.activeExecutions.remove(message.data.executionId, message.data.result);
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-29 05:12:54 -07:00
|
|
|
// Also get informed when the processes does exit especially when it did crash or timed out
|
2021-04-17 07:44:07 -07:00
|
|
|
subprocess.on('exit', async (code, signal) => {
|
2021-08-29 11:58:11 -07:00
|
|
|
if (signal === 'SIGTERM') {
|
|
|
|
Logger.debug(`Subprocess for execution ID ${executionId} timed out.`, { executionId });
|
2020-07-29 05:12:54 -07:00
|
|
|
// Execution timed out and its process has been terminated
|
2021-04-16 09:33:36 -07:00
|
|
|
const timeoutError = new WorkflowOperationError('Workflow execution timed out!');
|
2020-07-29 05:12:54 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
await this.processError(
|
|
|
|
timeoutError,
|
|
|
|
startedAt,
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
workflowHooks,
|
|
|
|
);
|
2020-07-29 05:12:54 -07:00
|
|
|
} else if (code !== 0) {
|
2021-08-29 11:58:11 -07:00
|
|
|
Logger.debug(
|
|
|
|
`Subprocess for execution ID ${executionId} finished with error code ${code}.`,
|
|
|
|
{ executionId },
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
// Process did exit with error code, so something went wrong.
|
2021-08-29 11:58:11 -07:00
|
|
|
const executionError = new WorkflowOperationError(
|
|
|
|
'Workflow execution process did crash for an unknown reason!',
|
|
|
|
);
|
|
|
|
|
|
|
|
await this.processError(
|
|
|
|
executionError,
|
|
|
|
startedAt,
|
|
|
|
data.executionMode,
|
|
|
|
executionId,
|
|
|
|
workflowHooks,
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
2021-04-17 07:44:07 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
for (const executionId of childExecutionIds) {
|
2021-04-17 07:44:07 -07:00
|
|
|
// When the child process exits, if we still have
|
|
|
|
// pending child executions, we mark them as finished
|
|
|
|
// They will display as unknown to the user
|
|
|
|
// Instead of pending forever as executing when it
|
|
|
|
// actually isn't anymore.
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/await-thenable, no-await-in-loop
|
2021-04-17 07:44:07 -07:00
|
|
|
await this.activeExecutions.remove(executionId);
|
|
|
|
}
|
|
|
|
|
2020-07-29 05:12:54 -07:00
|
|
|
clearTimeout(executionTimeout);
|
2019-08-08 11:38:25 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return executionId;
|
|
|
|
}
|
|
|
|
}
|