2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
|
|
/* eslint-disable consistent-return */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
|
|
/* eslint-disable @typescript-eslint/no-shadow */
|
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
|
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
|
|
/* eslint-disable @typescript-eslint/unbound-method */
|
2021-12-23 13:29:04 -08:00
|
|
|
import {
|
|
|
|
BinaryDataManager,
|
|
|
|
IBinaryDataConfig,
|
|
|
|
IProcessMessage,
|
|
|
|
UserSettings,
|
|
|
|
WorkflowExecute,
|
|
|
|
} from 'n8n-core';
|
2019-08-08 11:38:25 -07:00
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
ExecutionError,
|
2019-08-08 11:38:25 -07:00
|
|
|
IDataObject,
|
2021-11-05 09:45:51 -07:00
|
|
|
IExecuteResponsePromiseData,
|
2021-02-20 04:51:06 -08:00
|
|
|
IExecuteWorkflowInfo,
|
2021-05-01 20:43:01 -07:00
|
|
|
ILogger,
|
2021-02-20 04:51:06 -08:00
|
|
|
INodeExecutionData,
|
2019-08-08 11:38:25 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeData,
|
|
|
|
IRun,
|
|
|
|
ITaskData,
|
2021-02-20 04:51:06 -08:00
|
|
|
IWorkflowExecuteAdditionalData,
|
2020-11-13 14:31:27 -08:00
|
|
|
IWorkflowExecuteHooks,
|
2021-05-01 20:43:01 -07:00
|
|
|
LoggerProxy,
|
2019-08-08 11:38:25 -07:00
|
|
|
Workflow,
|
2021-07-10 02:34:41 -07:00
|
|
|
WorkflowExecuteMode,
|
2019-12-19 14:07:55 -08:00
|
|
|
WorkflowHooks,
|
2021-04-16 09:33:36 -07:00
|
|
|
WorkflowOperationError,
|
2019-08-08 11:38:25 -07:00
|
|
|
} from 'n8n-workflow';
|
2021-05-01 20:43:01 -07:00
|
|
|
import {
|
2021-08-29 11:58:11 -07:00
|
|
|
CredentialsOverwrites,
|
|
|
|
CredentialTypes,
|
|
|
|
Db,
|
|
|
|
ExternalHooks,
|
2021-12-10 06:29:05 -08:00
|
|
|
GenericHelpers,
|
2021-08-29 11:58:11 -07:00
|
|
|
IWorkflowExecuteProcess,
|
|
|
|
IWorkflowExecutionDataProcessWithExecution,
|
|
|
|
NodeTypes,
|
2021-11-05 09:45:51 -07:00
|
|
|
WebhookHelpers,
|
2021-08-29 11:58:11 -07:00
|
|
|
WorkflowExecuteAdditionalData,
|
|
|
|
WorkflowHelpers,
|
|
|
|
} from '.';
|
|
|
|
|
|
|
|
import { getLogger } from './Logger';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2021-06-23 02:20:07 -07:00
|
|
|
import * as config from '../config';
|
2021-10-18 20:57:49 -07:00
|
|
|
import { InternalHooksManager } from './InternalHooksManager';
|
2021-02-08 23:59:32 -08:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
export class WorkflowRunnerProcess {
|
|
|
|
data: IWorkflowExecutionDataProcessWithExecution | undefined;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2021-05-01 20:43:01 -07:00
|
|
|
logger: ILogger;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
startedAt = new Date();
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
workflow: Workflow | undefined;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
workflowExecute: WorkflowExecute | undefined;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
2021-02-20 04:51:06 -08:00
|
|
|
executionIdCallback: (executionId: string) => void | undefined;
|
2021-08-29 11:58:11 -07:00
|
|
|
|
2021-04-17 07:44:07 -07:00
|
|
|
childExecutions: {
|
2021-08-29 11:58:11 -07:00
|
|
|
[key: string]: IWorkflowExecuteProcess;
|
2021-04-17 07:44:07 -07:00
|
|
|
} = {};
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-03-10 06:51:18 -08:00
|
|
|
static async stopProcess() {
|
|
|
|
setTimeout(() => {
|
|
|
|
// Attempt a graceful shutdown, giving executions 30 seconds to finish
|
|
|
|
process.exit(0);
|
|
|
|
}, 30000);
|
|
|
|
}
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
async runWorkflow(inputData: IWorkflowExecutionDataProcessWithExecution): Promise<IRun> {
|
2021-03-10 06:51:18 -08:00
|
|
|
process.on('SIGTERM', WorkflowRunnerProcess.stopProcess);
|
|
|
|
process.on('SIGINT', WorkflowRunnerProcess.stopProcess);
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-multi-assign
|
|
|
|
const logger = (this.logger = getLogger());
|
2021-05-01 20:43:01 -07:00
|
|
|
LoggerProxy.init(logger);
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
this.data = inputData;
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
logger.verbose('Initializing n8n sub-process', {
|
|
|
|
pid: process.pid,
|
|
|
|
workflowId: this.data.workflowData.id,
|
|
|
|
});
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
let className: string;
|
|
|
|
let tempNode: INodeType;
|
|
|
|
let filePath: string;
|
|
|
|
|
|
|
|
this.startedAt = new Date();
|
|
|
|
|
|
|
|
const nodeTypesData: INodeTypeData = {};
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2019-08-08 11:38:25 -07:00
|
|
|
for (const nodeTypeName of Object.keys(this.data.nodeTypeData)) {
|
|
|
|
className = this.data.nodeTypeData[nodeTypeName].className;
|
|
|
|
|
|
|
|
filePath = this.data.nodeTypeData[nodeTypeName].sourcePath;
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires
|
2019-08-08 11:38:25 -07:00
|
|
|
const tempModule = require(filePath);
|
|
|
|
|
|
|
|
try {
|
2021-09-21 10:38:24 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
const nodeObject = new tempModule[className]();
|
|
|
|
if (nodeObject.getNodeType !== undefined) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
|
|
tempNode = nodeObject.getNodeType();
|
|
|
|
} else {
|
|
|
|
tempNode = nodeObject;
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
2019-08-08 11:38:25 -07:00
|
|
|
tempNode = new tempModule[className]() as INodeType;
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Error loading node "${nodeTypeName}" from: "${filePath}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeTypesData[nodeTypeName] = {
|
|
|
|
type: tempNode,
|
|
|
|
sourcePath: filePath,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const nodeTypes = NodeTypes();
|
|
|
|
await nodeTypes.init(nodeTypesData);
|
|
|
|
|
2020-05-14 05:27:19 -07:00
|
|
|
// Init credential types the workflow uses (is needed to apply default values to credentials)
|
|
|
|
const credentialTypes = CredentialTypes();
|
|
|
|
await credentialTypes.init(inputData.credentialsTypeData);
|
|
|
|
|
|
|
|
// Load the credentials overwrites if any exist
|
|
|
|
const credentialsOverwrites = CredentialsOverwrites();
|
2020-09-12 12:13:57 -07:00
|
|
|
await credentialsOverwrites.init(inputData.credentialsOverwrite);
|
2020-05-14 05:27:19 -07:00
|
|
|
|
2020-11-13 14:31:27 -08:00
|
|
|
// Load all external hooks
|
|
|
|
const externalHooks = ExternalHooks();
|
|
|
|
await externalHooks.init();
|
|
|
|
|
2021-10-18 20:57:49 -07:00
|
|
|
const instanceId = (await UserSettings.prepareUserSettings()).instanceId ?? '';
|
2021-12-10 06:29:05 -08:00
|
|
|
const { cli } = await GenericHelpers.getVersions();
|
2022-01-07 08:14:59 -08:00
|
|
|
InternalHooksManager.init(instanceId, cli, nodeTypes);
|
2021-10-18 20:57:49 -07:00
|
|
|
|
2021-12-23 13:29:04 -08:00
|
|
|
const binaryDataConfig = config.get('binaryDataManager') as IBinaryDataConfig;
|
|
|
|
await BinaryDataManager.init(binaryDataConfig);
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
// Credentials should now be loaded from database.
|
|
|
|
// We check if any node uses credentials. If it does, then
|
|
|
|
// init database.
|
|
|
|
let shouldInitializaDb = false;
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line array-callback-return
|
|
|
|
inputData.workflowData.nodes.map((node) => {
|
2021-08-20 09:57:30 -07:00
|
|
|
if (Object.keys(node.credentials === undefined ? {} : node.credentials).length > 0) {
|
|
|
|
shouldInitializaDb = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// This code has been split into 4 ifs just to make it easier to understand
|
2021-02-08 23:59:32 -08:00
|
|
|
// Can be made smaller but in the end it will make it impossible to read.
|
2021-08-20 09:57:30 -07:00
|
|
|
if (shouldInitializaDb) {
|
|
|
|
// initialize db as we need to load credentials
|
|
|
|
await Db.init();
|
2021-08-29 11:58:11 -07:00
|
|
|
} else if (
|
|
|
|
inputData.workflowData.settings !== undefined &&
|
|
|
|
inputData.workflowData.settings.saveExecutionProgress === true
|
|
|
|
) {
|
2021-02-08 23:59:32 -08:00
|
|
|
// Workflow settings specifying it should save
|
|
|
|
await Db.init();
|
2021-08-29 11:58:11 -07:00
|
|
|
} else if (
|
|
|
|
inputData.workflowData.settings !== undefined &&
|
|
|
|
inputData.workflowData.settings.saveExecutionProgress !== false &&
|
|
|
|
(config.get('executions.saveExecutionProgress') as boolean)
|
|
|
|
) {
|
2021-02-08 23:59:32 -08:00
|
|
|
// Workflow settings not saying anything about saving but default settings says so
|
|
|
|
await Db.init();
|
2021-08-29 11:58:11 -07:00
|
|
|
} else if (
|
|
|
|
inputData.workflowData.settings === undefined &&
|
|
|
|
(config.get('executions.saveExecutionProgress') as boolean)
|
|
|
|
) {
|
2021-02-08 23:59:32 -08:00
|
|
|
// Workflow settings not saying anything about saving but default settings says so
|
|
|
|
await Db.init();
|
|
|
|
}
|
|
|
|
|
2021-04-17 07:44:07 -07:00
|
|
|
// Start timeout for the execution
|
|
|
|
let workflowTimeout = config.get('executions.timeout') as number; // initialize with default
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
|
2021-04-17 07:44:07 -07:00
|
|
|
if (this.data.workflowData.settings && this.data.workflowData.settings.executionTimeout) {
|
2021-08-29 11:58:11 -07:00
|
|
|
workflowTimeout = this.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
|
|
|
this.workflow = new Workflow({
|
|
|
|
id: this.data.workflowData.id as string | undefined,
|
|
|
|
name: this.data.workflowData.name,
|
|
|
|
nodes: this.data.workflowData.nodes,
|
|
|
|
connections: this.data.workflowData.connections,
|
|
|
|
active: this.data.workflowData.active,
|
|
|
|
nodeTypes,
|
|
|
|
staticData: this.data.workflowData.staticData,
|
|
|
|
settings: this.data.workflowData.settings,
|
|
|
|
});
|
|
|
|
const additionalData = await WorkflowExecuteAdditionalData.getBase(
|
|
|
|
undefined,
|
|
|
|
workflowTimeout <= 0 ? undefined : Date.now() + workflowTimeout * 1000,
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
additionalData.hooks = this.getProcessForwardHooks();
|
2021-11-05 09:45:51 -07:00
|
|
|
|
|
|
|
additionalData.hooks.hookFunctions.sendResponse = [
|
|
|
|
async (response: IExecuteResponsePromiseData): Promise<void> => {
|
|
|
|
await sendToParentProcess('sendResponse', {
|
|
|
|
response: WebhookHelpers.encodeWebhookResponse(response),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-08-21 05:11:32 -07:00
|
|
|
additionalData.executionId = inputData.executionId;
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
additionalData.sendMessageToUI = async (source: string, message: any) => {
|
2021-05-29 11:41:25 -07:00
|
|
|
if (workflowRunner.data!.executionMode !== 'manual') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
2021-05-29 11:41:25 -07:00
|
|
|
await sendToParentProcess('sendMessageToUI', { source, message });
|
|
|
|
} catch (error) {
|
2021-08-29 11:58:11 -07:00
|
|
|
this.logger.error(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
|
|
|
|
`There was a problem sending UI data to parent process: "${error.message}"`,
|
|
|
|
);
|
2021-05-29 11:41:25 -07:00
|
|
|
}
|
|
|
|
};
|
2021-02-20 04:51:06 -08:00
|
|
|
const executeWorkflowFunction = additionalData.executeWorkflow;
|
2021-08-29 11:58:11 -07:00
|
|
|
additionalData.executeWorkflow = async (
|
|
|
|
workflowInfo: IExecuteWorkflowInfo,
|
|
|
|
additionalData: IWorkflowExecuteAdditionalData,
|
|
|
|
inputData?: INodeExecutionData[] | undefined,
|
|
|
|
): Promise<Array<INodeExecutionData[] | null> | IRun> => {
|
2021-02-20 04:51:06 -08:00
|
|
|
const workflowData = await WorkflowExecuteAdditionalData.getWorkflowData(workflowInfo);
|
|
|
|
const runData = await WorkflowExecuteAdditionalData.getRunData(workflowData, inputData);
|
|
|
|
await sendToParentProcess('startExecution', { runData });
|
|
|
|
const executionId: string = await new Promise((resolve) => {
|
|
|
|
this.executionIdCallback = (executionId: string) => {
|
|
|
|
resolve(executionId);
|
|
|
|
};
|
|
|
|
});
|
2021-03-18 15:24:57 -07:00
|
|
|
let result: IRun;
|
|
|
|
try {
|
2021-08-29 11:58:11 -07:00
|
|
|
const executeWorkflowFunctionOutput = (await executeWorkflowFunction(
|
|
|
|
workflowInfo,
|
|
|
|
additionalData,
|
|
|
|
inputData,
|
|
|
|
executionId,
|
|
|
|
workflowData,
|
|
|
|
runData,
|
|
|
|
)) as { workflowExecute: WorkflowExecute; workflow: Workflow } as IWorkflowExecuteProcess;
|
|
|
|
const { workflowExecute } = executeWorkflowFunctionOutput;
|
2021-04-17 07:44:07 -07:00
|
|
|
this.childExecutions[executionId] = executeWorkflowFunctionOutput;
|
2021-08-29 11:58:11 -07:00
|
|
|
const { workflow } = executeWorkflowFunctionOutput;
|
|
|
|
result = await workflowExecute.processRunExecutionData(workflow);
|
2021-12-25 04:42:17 -08:00
|
|
|
await externalHooks.run('workflow.postExecute', [result, workflowData, executionId]);
|
2021-12-23 13:29:04 -08:00
|
|
|
void InternalHooksManager.getInstance().onWorkflowPostExecute(
|
|
|
|
executionId,
|
|
|
|
workflowData,
|
|
|
|
result,
|
|
|
|
);
|
2021-04-17 07:44:07 -07:00
|
|
|
await sendToParentProcess('finishExecution', { executionId, result });
|
|
|
|
delete this.childExecutions[executionId];
|
2021-03-18 15:24:57 -07:00
|
|
|
} catch (e) {
|
|
|
|
await sendToParentProcess('finishExecution', { executionId });
|
2021-04-17 07:44:07 -07:00
|
|
|
delete this.childExecutions[executionId];
|
|
|
|
// Throw same error we had
|
|
|
|
throw e;
|
2021-03-18 15:24:57 -07:00
|
|
|
}
|
2021-02-20 04:51:06 -08:00
|
|
|
|
2021-05-01 20:43:01 -07:00
|
|
|
await sendToParentProcess('finishExecution', { executionId, result });
|
|
|
|
|
2021-02-20 04:51:06 -08:00
|
|
|
const returnData = WorkflowHelpers.getDataLastExecutedNodeData(result);
|
|
|
|
return returnData!.data!.main;
|
|
|
|
};
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
if (this.data.executionData !== undefined) {
|
2021-08-29 11:58:11 -07:00
|
|
|
this.workflowExecute = new WorkflowExecute(
|
|
|
|
additionalData,
|
|
|
|
this.data.executionMode,
|
|
|
|
this.data.executionData,
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
return this.workflowExecute.processRunExecutionData(this.workflow);
|
2021-08-29 11:58:11 -07:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
this.data.runData === undefined ||
|
|
|
|
this.data.startNodes === undefined ||
|
|
|
|
this.data.startNodes.length === 0 ||
|
|
|
|
this.data.destinationNode === undefined
|
|
|
|
) {
|
2019-08-08 11:38:25 -07:00
|
|
|
// Execute all nodes
|
2021-02-20 04:51:06 -08:00
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
// Can execute without webhook so go on
|
|
|
|
this.workflowExecute = new WorkflowExecute(additionalData, this.data.executionMode);
|
|
|
|
return this.workflowExecute.run(this.workflow, undefined, this.data.destinationNode);
|
|
|
|
}
|
2021-08-29 11:58:11 -07:00
|
|
|
// Execute only the nodes between start and destination nodes
|
|
|
|
this.workflowExecute = new WorkflowExecute(additionalData, this.data.executionMode);
|
|
|
|
return this.workflowExecute.runPartialWorkflow(
|
|
|
|
this.workflow,
|
|
|
|
this.data.runData,
|
|
|
|
this.data.startNodes,
|
|
|
|
this.data.destinationNode,
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:05:15 -07:00
|
|
|
/**
|
|
|
|
* Sends hook data to the parent process that it executes them
|
|
|
|
*
|
|
|
|
* @param {string} hook
|
|
|
|
* @param {any[]} parameters
|
|
|
|
* @memberof WorkflowRunnerProcess
|
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
|
|
|
|
async sendHookToParentProcess(hook: string, parameters: any[]) {
|
2019-08-09 12:05:15 -07:00
|
|
|
try {
|
2021-05-11 20:15:22 -07:00
|
|
|
await sendToParentProcess('processHook', {
|
2019-08-08 11:38:25 -07:00
|
|
|
hook,
|
|
|
|
parameters,
|
2019-08-09 12:05:15 -07:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2021-08-29 11:58:11 -07:00
|
|
|
this.logger.error(`There was a problem sending hook: "${hook}"`, { parameters, error });
|
2019-08-09 12:05:15 -07:00
|
|
|
}
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a wrapper for hooks which simply forwards the data to
|
|
|
|
* the parent process where they then can be executed with access
|
|
|
|
* to database and to PushService
|
|
|
|
*
|
|
|
|
* @returns
|
|
|
|
*/
|
2019-12-19 14:07:55 -08:00
|
|
|
getProcessForwardHooks(): WorkflowHooks {
|
2020-11-13 14:31:27 -08:00
|
|
|
const hookFunctions: IWorkflowExecuteHooks = {
|
2019-08-08 11:38:25 -07:00
|
|
|
nodeExecuteBefore: [
|
|
|
|
async (nodeName: string): Promise<void> => {
|
2021-05-11 20:15:22 -07:00
|
|
|
await this.sendHookToParentProcess('nodeExecuteBefore', [nodeName]);
|
2019-08-08 11:38:25 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
nodeExecuteAfter: [
|
2021-03-10 01:50:07 -08:00
|
|
|
async (nodeName: string, data: ITaskData): Promise<void> => {
|
2021-05-11 20:15:22 -07:00
|
|
|
await this.sendHookToParentProcess('nodeExecuteAfter', [nodeName, data]);
|
2019-08-08 11:38:25 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
workflowExecuteBefore: [
|
|
|
|
async (): Promise<void> => {
|
2021-05-11 20:15:22 -07:00
|
|
|
await this.sendHookToParentProcess('workflowExecuteBefore', []);
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-08-08 11:38:25 -07:00
|
|
|
],
|
|
|
|
workflowExecuteAfter: [
|
|
|
|
async (fullRunData: IRun, newStaticData?: IDataObject): Promise<void> => {
|
2021-05-11 20:15:22 -07:00
|
|
|
await this.sendHookToParentProcess('workflowExecuteAfter', [fullRunData, newStaticData]);
|
2019-08-08 11:38:25 -07:00
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2019-08-08 11:38:25 -07:00
|
|
|
};
|
2019-12-19 14:07:55 -08:00
|
|
|
|
2020-11-13 14:31:27 -08:00
|
|
|
const preExecuteFunctions = WorkflowExecuteAdditionalData.hookFunctionsPreExecute();
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2020-11-13 14:31:27 -08:00
|
|
|
for (const key of Object.keys(preExecuteFunctions)) {
|
2021-02-08 23:59:32 -08:00
|
|
|
if (hookFunctions[key] === undefined) {
|
|
|
|
hookFunctions[key] = [];
|
|
|
|
}
|
2020-11-13 14:31:27 -08:00
|
|
|
hookFunctions[key]!.push.apply(hookFunctions[key], preExecuteFunctions[key]);
|
|
|
|
}
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
return new WorkflowHooks(
|
|
|
|
hookFunctions,
|
|
|
|
this.data!.executionMode,
|
|
|
|
this.data!.executionId,
|
|
|
|
this.data!.workflowData,
|
|
|
|
{ sessionId: this.data!.sessionId, retryOf: this.data!.retryOf as string },
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends data to parent process
|
|
|
|
*
|
|
|
|
* @param {string} type The type of data to send
|
|
|
|
* @param {*} data The data
|
2019-08-09 02:07:54 -07:00
|
|
|
* @returns {Promise<void>}
|
2019-08-08 11:38:25 -07:00
|
|
|
*/
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
async function sendToParentProcess(type: string, data: any): Promise<void> {
|
2019-08-09 02:07:54 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-08-29 11:58:11 -07:00
|
|
|
process.send!(
|
|
|
|
{
|
|
|
|
type,
|
|
|
|
data,
|
|
|
|
},
|
|
|
|
(error: Error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
2019-08-09 02:07:54 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const workflowRunner = new WorkflowRunnerProcess();
|
|
|
|
|
|
|
|
// Listen to messages from parent process which send the data of
|
|
|
|
// the worflow to process
|
|
|
|
process.on('message', async (message: IProcessMessage) => {
|
|
|
|
try {
|
|
|
|
if (message.type === 'startWorkflow') {
|
2021-04-17 07:44:07 -07:00
|
|
|
await sendToParentProcess('start', {});
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
const runData = await workflowRunner.runWorkflow(message.data);
|
|
|
|
|
2019-08-09 02:07:54 -07:00
|
|
|
await sendToParentProcess('end', {
|
2019-08-08 11:38:25 -07:00
|
|
|
runData,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Once the workflow got executed make sure the process gets killed again
|
|
|
|
process.exit();
|
2020-07-29 05:12:54 -07:00
|
|
|
} else if (message.type === 'stopExecution' || message.type === 'timeout') {
|
2019-08-08 11:38:25 -07:00
|
|
|
// The workflow execution should be stopped
|
2019-08-09 12:05:15 -07:00
|
|
|
let runData: IRun;
|
2019-08-08 11:38:25 -07:00
|
|
|
|
|
|
|
if (workflowRunner.workflowExecute !== undefined) {
|
2021-04-17 07:44:07 -07:00
|
|
|
const executionIds = Object.keys(workflowRunner.childExecutions);
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2021-04-17 07:44:07 -07:00
|
|
|
for (const executionId of executionIds) {
|
|
|
|
const childWorkflowExecute = workflowRunner.childExecutions[executionId];
|
2021-08-29 11:58:11 -07:00
|
|
|
runData = childWorkflowExecute.workflowExecute.getFullRunData(
|
|
|
|
workflowRunner.childExecutions[executionId].startedAt,
|
|
|
|
);
|
|
|
|
const timeOutError =
|
|
|
|
message.type === 'timeout'
|
|
|
|
? new WorkflowOperationError('Workflow execution timed out!')
|
|
|
|
: new WorkflowOperationError('Workflow-Execution has been canceled!');
|
2021-04-17 07:44:07 -07:00
|
|
|
|
|
|
|
// If there is any data send it to parent process, if execution timedout add the error
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await childWorkflowExecute.workflowExecute.processSuccessExecution(
|
|
|
|
workflowRunner.childExecutions[executionId].startedAt,
|
|
|
|
childWorkflowExecute.workflow,
|
|
|
|
timeOutError,
|
|
|
|
);
|
2021-04-17 07:44:07 -07:00
|
|
|
}
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
// Workflow started already executing
|
2019-08-09 12:05:15 -07:00
|
|
|
runData = workflowRunner.workflowExecute.getFullRunData(workflowRunner.startedAt);
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
const timeOutError =
|
|
|
|
message.type === 'timeout'
|
|
|
|
? new WorkflowOperationError('Workflow execution timed out!')
|
|
|
|
: new WorkflowOperationError('Workflow-Execution has been canceled!');
|
2020-07-29 05:12:54 -07:00
|
|
|
|
|
|
|
// If there is any data send it to parent process, if execution timedout add the error
|
2021-08-29 11:58:11 -07:00
|
|
|
await workflowRunner.workflowExecute.processSuccessExecution(
|
|
|
|
workflowRunner.startedAt,
|
|
|
|
workflowRunner.workflow!,
|
|
|
|
timeOutError,
|
|
|
|
);
|
2019-08-08 11:38:25 -07:00
|
|
|
} else {
|
|
|
|
// Workflow did not get started yet
|
2019-08-09 12:05:15 -07:00
|
|
|
runData = {
|
2019-08-08 11:38:25 -07:00
|
|
|
data: {
|
|
|
|
resultData: {
|
|
|
|
runData: {},
|
|
|
|
},
|
|
|
|
},
|
2021-07-10 02:34:41 -07:00
|
|
|
finished: false,
|
2021-08-29 11:58:11 -07:00
|
|
|
mode: workflowRunner.data
|
|
|
|
? workflowRunner.data.executionMode
|
|
|
|
: ('own' as WorkflowExecuteMode),
|
2019-08-08 11:38:25 -07:00
|
|
|
startedAt: workflowRunner.startedAt,
|
|
|
|
stoppedAt: new Date(),
|
|
|
|
};
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2019-08-09 12:05:15 -07:00
|
|
|
workflowRunner.sendHookToParentProcess('workflowExecuteAfter', [runData]);
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 05:12:54 -07:00
|
|
|
await sendToParentProcess(message.type === 'timeout' ? message.type : 'end', {
|
2019-08-09 12:05:15 -07:00
|
|
|
runData,
|
2019-08-08 11:38:25 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Stop process
|
|
|
|
process.exit();
|
2021-02-20 04:51:06 -08:00
|
|
|
} else if (message.type === 'executionId') {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
2021-02-20 04:51:06 -08:00
|
|
|
workflowRunner.executionIdCallback(message.data.executionId);
|
2019-08-08 11:38:25 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Catch all uncaught errors and forward them to parent process
|
|
|
|
const executionError = {
|
2021-04-16 09:33:36 -07:00
|
|
|
...error,
|
2021-08-29 11:58:11 -07:00
|
|
|
name: error.name || 'Error',
|
|
|
|
message: error.message,
|
|
|
|
stack: error.stack,
|
2021-04-16 09:33:36 -07:00
|
|
|
} as ExecutionError;
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2019-08-09 02:07:54 -07:00
|
|
|
await sendToParentProcess('processError', {
|
2019-08-08 11:38:25 -07:00
|
|
|
executionError,
|
|
|
|
});
|
|
|
|
process.exit();
|
|
|
|
}
|
2019-08-09 02:07:54 -07:00
|
|
|
});
|