1
0
Fork 0
mirror of https://github.com/n8n-io/n8n.git synced 2025-03-05 20:50:17 -08:00

Simplify code and fix issue with stopping process

This commit is contained in:
Jan Oberhauser 2019-08-09 21:05:15 +02:00
parent fcb0982401
commit 135431ebcd

View file

@ -29,6 +29,7 @@ export class WorkflowRunnerProcess {
workflow: Workflow | undefined; workflow: Workflow | undefined;
workflowExecute: WorkflowExecute | undefined; workflowExecute: WorkflowExecute | undefined;
async runWorkflow(inputData: IWorkflowExecutionDataProcessWithExecution): Promise<IRun> { async runWorkflow(inputData: IWorkflowExecutionDataProcessWithExecution): Promise<IRun> {
this.data = inputData; this.data = inputData;
let className: string; let className: string;
@ -81,15 +82,29 @@ export class WorkflowRunnerProcess {
} }
/**
* Sends hook data to the parent process that it executes them
*
* @param {string} hook
* @param {any[]} parameters
* @memberof WorkflowRunnerProcess
*/
sendHookToParentProcess(hook: string, parameters: any[]) { // tslint:disable-line:no-any sendHookToParentProcess(hook: string, parameters: any[]) { // tslint:disable-line:no-any
(process as unknown as ChildProcess).send({ try {
type: 'processHook', sendToParentProcess('processHook', {
data: {
hook, hook,
parameters, parameters,
} as IProcessMessageDataHook, });
} as IProcessMessage); } catch (error) {
// TODO: Add proper logging
console.error(`There was a problem sending hook: "${hook}"`);
console.error('Parameters:');
console.error(parameters);
console.error('Error:');
console.error(error);
} }
}
/** /**
* Create a wrapper for hooks which simply forwards the data to * Create a wrapper for hooks which simply forwards the data to
@ -169,18 +184,18 @@ process.on('message', async (message: IProcessMessage) => {
process.exit(); process.exit();
} else if (message.type === 'stopExecution') { } else if (message.type === 'stopExecution') {
// The workflow execution should be stopped // The workflow execution should be stopped
let fullRunData: IRun; let runData: IRun;
if (workflowRunner.workflowExecute !== undefined) { if (workflowRunner.workflowExecute !== undefined) {
// Workflow started already executing // Workflow started already executing
fullRunData = workflowRunner.workflowExecute.getFullRunData(workflowRunner.startedAt); runData = workflowRunner.workflowExecute.getFullRunData(workflowRunner.startedAt);
// If there is any data send it to parent process // If there is any data send it to parent process
await workflowRunner.workflowExecute.processSuccessExecution(workflowRunner.startedAt, workflowRunner.workflow!); await workflowRunner.workflowExecute.processSuccessExecution(workflowRunner.startedAt, workflowRunner.workflow!);
} else { } else {
// Workflow did not get started yet // Workflow did not get started yet
fullRunData = { runData = {
data: { data: {
resultData: { resultData: {
runData: {}, runData: {},
@ -192,11 +207,11 @@ process.on('message', async (message: IProcessMessage) => {
stoppedAt: new Date(), stoppedAt: new Date(),
}; };
workflowRunner.sendHookToParentProcess('workflowExecuteAfter', [fullRunData]); workflowRunner.sendHookToParentProcess('workflowExecuteAfter', [runData]);
} }
await sendToParentProcess('end', { await sendToParentProcess('end', {
fullRunData, runData,
}); });
// Stop process // Stop process