fix: Ensure external hooks post workflow execute run in queue mode (#7947)

## Summary
Since 1.8.x a refactor removed the call to `workflow.postExecute`'s
External hook from the execution path. This PR adds it back to the
correct place, where workers are supposed to call this, allowing us to
avoid having to re-read the execution data in the caller just for the
hooks.

It is important to have the hooks running in the worker whenever
possible to prevent having to read the full execution data in the
caller.

#### How to test the change:
1. Use the attached hooks file
[external-hooks.txt](https://github.com/n8n-io/n8n/files/13597270/external-hooks.txt)
setting it up via environment variable using `export
EXTERNAL_HOOK_FILES=/path/to/hooks/external-hooks.js`
2. Set up queue mode loading this file in both main and workers
3. See that the message logs will not be displayed without this fix


## Issues fixed
Include links to Github issue or Community forum post or **Linear
ticket**:



## Review / Merge checklist
- [ ] PR title and summary are descriptive. **Remember, the title
automatically goes into the changelog. Use `(no-changelog)` otherwise.**
([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md))
- [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up
ticket created.
- [ ] Tests included.
> A bug is not considered fixed, unless a test is added to prevent it
from happening again. A feature is not complete without tests.
  >
> *(internal)* You can use Slack commands to trigger [e2e
tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227)
or [deploy test
instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce)
or [deploy early access version on
Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
This commit is contained in:
Omar Ajoue 2023-12-07 10:00:05 +00:00 committed by GitHub
parent 386bd61967
commit 3ba7deb337
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -625,6 +625,21 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
// send tracking and event log events, but don't wait for them
void internalHooks.onWorkflowPostExecute(this.executionId, this.workflowData, fullRunData);
},
async function (this: WorkflowHooks, fullRunData: IRun, newStaticData: IDataObject) {
const externalHooks = Container.get(ExternalHooks);
if (externalHooks.exists('workflow.postExecute')) {
try {
await externalHooks.run('workflow.postExecute', [
fullRunData,
this.workflowData,
this.executionId,
]);
} catch (error) {
ErrorReporter.error(error);
console.error('There was a problem running hook "workflow.postExecute"', error);
}
}
},
],
nodeFetchedData: [
async (workflowId: string, node: INode) => {
@ -1014,6 +1029,7 @@ export function getWorkflowHooksWorkerExecuter(
}
hookFunctions[key]!.push.apply(hookFunctions[key], preExecuteFunctions[key]);
}
return new WorkflowHooks(hookFunctions, mode, executionId, workflowData, optionalParameters);
}

View file

@ -217,7 +217,11 @@ export class WorkflowRunner {
// only run these when not in queue mode or when the execution is manual,
// since these calls are now done by the worker directly
if (executionsMode !== 'queue' || data.executionMode === 'manual') {
if (
executionsMode !== 'queue' ||
config.getEnv('generic.instanceType') === 'worker' ||
data.executionMode === 'manual'
) {
const postExecutePromise = this.activeExecutions.getPostExecutePromise(executionId);
const externalHooks = Container.get(ExternalHooks);
postExecutePromise