mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(core): Sync hookFunctionsSave
and hookFunctionsSaveWorker
(#12740)
This commit is contained in:
parent
d1b6692736
commit
d410b8f5a7
|
@ -26,6 +26,7 @@ import { mockInstance } from '@test/mocking';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getWorkflowHooksMain,
|
getWorkflowHooksMain,
|
||||||
|
getWorkflowHooksWorkerExecuter,
|
||||||
getWorkflowHooksWorkerMain,
|
getWorkflowHooksWorkerMain,
|
||||||
} from '../workflow-execute-additional-data';
|
} from '../workflow-execute-additional-data';
|
||||||
|
|
||||||
|
@ -532,4 +533,85 @@ describe('Execution Lifecycle Hooks', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getWorkflowHooksWorkerExecuter', () => {
|
||||||
|
let hooks: WorkflowHooks;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
hooks = getWorkflowHooksWorkerExecuter(executionMode, executionId, workflowData, {
|
||||||
|
pushRef,
|
||||||
|
retryOf,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('saving static data', () => {
|
||||||
|
it('should skip saving static data for manual executions', async () => {
|
||||||
|
hooks.mode = 'manual';
|
||||||
|
|
||||||
|
await hooks.executeHookFunctions('workflowExecuteAfter', [successfulRun, staticData]);
|
||||||
|
|
||||||
|
expect(workflowStaticDataService.saveStaticDataById).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should save static data for prod executions', async () => {
|
||||||
|
hooks.mode = 'trigger';
|
||||||
|
|
||||||
|
await hooks.executeHookFunctions('workflowExecuteAfter', [successfulRun, staticData]);
|
||||||
|
|
||||||
|
expect(workflowStaticDataService.saveStaticDataById).toHaveBeenCalledWith(
|
||||||
|
workflowId,
|
||||||
|
staticData,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle static data saving errors', async () => {
|
||||||
|
hooks.mode = 'trigger';
|
||||||
|
const error = new Error('Static data save failed');
|
||||||
|
workflowStaticDataService.saveStaticDataById.mockRejectedValueOnce(error);
|
||||||
|
|
||||||
|
await hooks.executeHookFunctions('workflowExecuteAfter', [successfulRun, staticData]);
|
||||||
|
|
||||||
|
expect(errorReporter.error).toHaveBeenCalledWith(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('error workflow', () => {
|
||||||
|
it('should not execute error workflow for manual executions', async () => {
|
||||||
|
hooks.mode = 'manual';
|
||||||
|
|
||||||
|
await hooks.executeHookFunctions('workflowExecuteAfter', [failedRun, {}]);
|
||||||
|
|
||||||
|
expect(workflowExecutionService.executeErrorWorkflow).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should execute error workflow for failed non-manual executions', async () => {
|
||||||
|
hooks.mode = 'trigger';
|
||||||
|
const errorWorkflow = 'error-workflow-id';
|
||||||
|
workflowData.settings = { errorWorkflow };
|
||||||
|
const project = mock<Project>();
|
||||||
|
ownershipService.getWorkflowProjectCached.calledWith(workflowId).mockResolvedValue(project);
|
||||||
|
|
||||||
|
await hooks.executeHookFunctions('workflowExecuteAfter', [failedRun, {}]);
|
||||||
|
|
||||||
|
expect(workflowExecutionService.executeErrorWorkflow).toHaveBeenCalledWith(
|
||||||
|
errorWorkflow,
|
||||||
|
{
|
||||||
|
workflow: {
|
||||||
|
id: workflowId,
|
||||||
|
name: workflowData.name,
|
||||||
|
},
|
||||||
|
execution: {
|
||||||
|
id: executionId,
|
||||||
|
error: expressionError,
|
||||||
|
mode: 'trigger',
|
||||||
|
retryOf,
|
||||||
|
lastNodeExecuted: undefined,
|
||||||
|
url: `http://localhost:5678/workflow/${workflowId}/executions/${executionId}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
project,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -576,8 +576,11 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
|
||||||
executionId: this.executionId,
|
executionId: this.executionId,
|
||||||
workflowId: this.workflowData.id,
|
workflowId: this.workflowData.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const isManualMode = this.mode === 'manual';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (isWorkflowIdValid(this.workflowData.id) && newStaticData) {
|
if (!isManualMode && isWorkflowIdValid(this.workflowData.id) && newStaticData) {
|
||||||
// Workflow is saved so update in database
|
// Workflow is saved so update in database
|
||||||
try {
|
try {
|
||||||
await Container.get(WorkflowStaticDataService).saveStaticDataById(
|
await Container.get(WorkflowStaticDataService).saveStaticDataById(
|
||||||
|
@ -596,7 +599,11 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
|
||||||
const workflowStatusFinal = determineFinalExecutionStatus(fullRunData);
|
const workflowStatusFinal = determineFinalExecutionStatus(fullRunData);
|
||||||
fullRunData.status = workflowStatusFinal;
|
fullRunData.status = workflowStatusFinal;
|
||||||
|
|
||||||
if (workflowStatusFinal !== 'success' && workflowStatusFinal !== 'waiting') {
|
if (
|
||||||
|
!isManualMode &&
|
||||||
|
workflowStatusFinal !== 'success' &&
|
||||||
|
workflowStatusFinal !== 'waiting'
|
||||||
|
) {
|
||||||
executeErrorWorkflow(
|
executeErrorWorkflow(
|
||||||
this.workflowData,
|
this.workflowData,
|
||||||
fullRunData,
|
fullRunData,
|
||||||
|
@ -615,19 +622,25 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
|
||||||
retryOf: this.retryOf,
|
retryOf: this.retryOf,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// When going into the waiting state, store the pushRef in the execution-data
|
||||||
|
if (fullRunData.waitTill && isManualMode) {
|
||||||
|
fullExecutionData.data.pushRef = this.pushRef;
|
||||||
|
}
|
||||||
|
|
||||||
await updateExistingExecution({
|
await updateExistingExecution({
|
||||||
executionId: this.executionId,
|
executionId: this.executionId,
|
||||||
workflowId: this.workflowData.id,
|
workflowId: this.workflowData.id,
|
||||||
executionData: fullExecutionData,
|
executionData: fullExecutionData,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
executeErrorWorkflow(
|
if (!isManualMode)
|
||||||
this.workflowData,
|
executeErrorWorkflow(
|
||||||
fullRunData,
|
this.workflowData,
|
||||||
this.mode,
|
fullRunData,
|
||||||
this.executionId,
|
this.mode,
|
||||||
this.retryOf,
|
this.executionId,
|
||||||
);
|
this.retryOf,
|
||||||
|
);
|
||||||
} finally {
|
} finally {
|
||||||
workflowStatisticsService.emit('workflowExecutionCompleted', {
|
workflowStatisticsService.emit('workflowExecutionCompleted', {
|
||||||
workflowData: this.workflowData,
|
workflowData: this.workflowData,
|
||||||
|
|
Loading…
Reference in a new issue