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

refactor: make IPollFunctions emit consistent with trigger emit #4201

Merged
merged 4 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 30 additions & 11 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ export class ActiveWorkflowRunner {

/**
* Return poll function which gets the global functions from n8n-core
* and overwrites the __emit to be able to start it in subprocess
* and overwrites the emit to be able to start it in subprocess
*
*/
getExecutePollFunctions(
Expand All @@ -628,19 +628,38 @@ export class ActiveWorkflowRunner {
mode,
activation,
);
// eslint-disable-next-line no-underscore-dangle
returnFunctions.__emit = async (
data: INodeExecutionData[][] | ExecutionError,
): Promise<void> => {
if (data instanceof Error) {
await createErrorExecution(data, node, workflowData, workflow, mode);
this.executeErrorWorkflow(data, workflowData, mode);
return;
}
returnFunctions.__emit = (
data: INodeExecutionData[][],
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
donePromise?: IDeferredPromise<IRun | undefined>,
): void => {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Logger.debug(`Received event to trigger execution for workflow "${workflow.name}"`);
WorkflowHelpers.saveStaticData(workflow);
this.runWorkflow(workflowData, node, data, additionalData, mode);
const executePromise = this.runWorkflow(
workflowData,
node,
data,
additionalData,
mode,
responsePromise,
);

if (donePromise) {
executePromise.then((executionId) => {
activeExecutions
.getPostExecutePromise(executionId)
.then(donePromise.resolve)
.catch(donePromise.reject);
});
} else {
executePromise.catch(console.error);
}
};

returnFunctions.__emitError = async (error: ExecutionError): Promise<void> => {
await createErrorExecution(error, node, workflowData, workflow, mode);
this.executeErrorWorkflow(error, workflowData, mode);
};
return returnFunctions;
};
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/ActiveWorkflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export class ActiveWorkflows {
const pollResponse = await workflow.runPoll(node, pollFunctions);

if (pollResponse !== null) {
// eslint-disable-next-line no-underscore-dangle
pollFunctions.__emit(pollResponse);
}
} catch (error) {
Expand All @@ -170,8 +169,7 @@ export class ActiveWorkflows {
if (testingTrigger) {
throw error;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-underscore-dangle
pollFunctions.__emit(error);
pollFunctions.__emitError(error);
}
};

Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,12 @@ export function getExecutePollFunctions(
return ((workflow: Workflow, node: INode) => {
return {
__emit: (data: INodeExecutionData[][]): void => {
throw new Error('Overwrite NodeExecuteFunctions.getExecutePullFunctions.__emit function!');
throw new Error('Overwrite NodeExecuteFunctions.getExecutePollFunctions.__emit function!');
},
__emitError(error: Error) {
throw new Error(
'Overwrite NodeExecuteFunctions.getExecutePollFunctions.__emitError function!',
);
},
async getCredentials(type: string): Promise<ICredentialDataDecryptedObject> {
return getCredentials(workflow, node, type, additionalData, mode);
Expand Down
7 changes: 6 additions & 1 deletion packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,12 @@ export interface IHookFunctions {
}

export interface IPollFunctions {
__emit(data: INodeExecutionData[][] | NodeApiError): void;
__emit(
data: INodeExecutionData[][],
responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>,
donePromise?: IDeferredPromise<IRun>,
): void;
__emitError(error: Error, responsePromise?: IDeferredPromise<IExecuteResponsePromiseData>): void;
getCredentials(type: string): Promise<ICredentialDataDecryptedObject>;
getMode(): WorkflowExecuteMode;
getActivationMode(): WorkflowActivateMode;
Expand Down