Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Fix issue with n8n crashing when error in poll method #4008

Merged
merged 5 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
WorkflowActivationError,
WorkflowExecuteMode,
LoggerProxy as Logger,
WorkflowOperationError,
NodeApiError,
} from 'n8n-workflow';

import express from 'express';
Expand All @@ -51,6 +53,8 @@ import {
WorkflowHelpers,
WorkflowRunner,
ExternalHooks,
IExecutionDb,
IExecutionFlattedDb,
} from '.';
import config from '../config';
import { User } from './databases/entities/User';
Expand Down Expand Up @@ -625,6 +629,83 @@ export class ActiveWorkflowRunner {
return workflowRunner.run(runData, true, undefined, undefined, responsePromise);
}

/**
* Create an error execution
*
* @param {INode} node
* @param {IWorkflowDb} workflowData
* @param {Workflow} workflow
* @param {WorkflowExecuteMode} mode
* @returns
* @memberof ActiveWorkflowRunner
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
createErrorExecution(
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
error: ExecutionError,
node: INode,
workflowData: IWorkflowDb,
workflow: Workflow,
mode: WorkflowExecuteMode,
) {
const executionData: IRunExecutionData = {
startData: {
destinationNode: node.name,
runNodeFilter: [node.name],
},
executionData: {
contextData: {},
nodeExecutionStack: [
{
node,
data: {
main: [
[
{
json: {},
pairedItem: {
item: 0,
},
},
],
],
},
source: null,
},
],
waitingExecution: {},
waitingExecutionSource: {},
},
resultData: {
runData: {
[node.name]: [
{
startTime: 0,
executionTime: 0,
error,
source: [],
},
],
},
error,
lastNodeExecuted: node.name,
},
};

const fullExecutionData: IExecutionDb = {
data: executionData,
mode,
finished: false,
startedAt: new Date(),
workflowData,
workflowId: workflow.id,
stoppedAt: new Date(),
};

const execution = ResponseHelper.flattenExecutionData(fullExecutionData);

return Db.collections.Execution.save(execution as IExecutionFlattedDb);
}

/**
* Return poll function which gets the global functions from n8n-core
* and overwrites the __emit to be able to start it in subprocess
Expand All @@ -650,7 +731,14 @@ export class ActiveWorkflowRunner {
activation,
);
// eslint-disable-next-line no-underscore-dangle
returnFunctions.__emit = (data: INodeExecutionData[][]): void => {
returnFunctions.__emit = async (
data: INodeExecutionData[][] | ExecutionError,
): Promise<void> => {
if (data instanceof Error) {
await this.createErrorExecution(data, node, workflowData, workflow, mode);
this.executeErrorWorkflow(data, workflowData, mode);
return;
}
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Logger.debug(`Received event to trigger execution for workflow "${workflow.name}"`);
WorkflowHelpers.saveStaticData(workflow);
Expand Down
27 changes: 20 additions & 7 deletions packages/core/src/ActiveWorkflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
WorkflowActivateMode,
WorkflowActivationError,
WorkflowExecuteMode,
WorkflowOperationError,
NodeApiError,
} from 'n8n-workflow';

// eslint-disable-next-line import/no-cycle
Expand Down Expand Up @@ -163,24 +165,35 @@ export class ActiveWorkflows {

// Get all the trigger times
const cronTimes = (pollTimes.item || []).map(toCronExpression);

// The trigger function to execute when the cron-time got reached
const executeTrigger = async () => {
const executeTrigger = async (testingTrigger = false) => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Logger.debug(`Polling trigger initiated for workflow "${workflow.name}"`, {
workflowName: workflow.name,
workflowId: workflow.id,
});
const pollResponse = await workflow.runPoll(node, pollFunctions);

if (pollResponse !== null) {
// eslint-disable-next-line no-underscore-dangle
pollFunctions.__emit(pollResponse);
try {
const pollResponse = await workflow.runPoll(node, pollFunctions);

if (pollResponse !== null) {
// eslint-disable-next-line no-underscore-dangle
pollFunctions.__emit(pollResponse);
}
} catch (error) {
// If the poll function failes in the first activation
// throw the error back so we let the user know there is
// an issue with the trigger.
if (testingTrigger) {
throw error;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-underscore-dangle
pollFunctions.__emit(error);
}
};

// Execute the trigger directly to be able to know if it works
await executeTrigger();
await executeTrigger(true);

const timezone = pollFunctions.getTimezone();

Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ export interface IHookFunctions {
}

export interface IPollFunctions {
__emit(data: INodeExecutionData[][]): void;
__emit(data: INodeExecutionData[][] | NodeApiError): void;
getCredentials(type: string): Promise<ICredentialDataDecryptedObject>;
getMode(): WorkflowExecuteMode;
getActivationMode(): WorkflowActivateMode;
Expand Down