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(core): Restore log event n8n.workflow.failed #10253

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions packages/cli/src/WorkflowExecuteAdditionalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,12 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
executionId,
success: runData.status === 'success',
isManual: runData.mode === 'manual',
lastNodeExecuted: runData?.data.resultData.lastNodeExecuted,
errorNodeType:
runData?.data.resultData.error && 'node' in runData?.data.resultData.error
? runData?.data.resultData.error.node?.type
: undefined,
errorMessage: runData?.data.resultData.error?.message.toString(),
ivov marked this conversation as resolved.
Show resolved Hide resolved
});
},
async function (this: WorkflowHooks, fullRunData: IRun) {
Expand Down Expand Up @@ -940,6 +946,12 @@ async function executeWorkflow(
success: data.status === 'success',
isManual: data.mode === 'manual',
userId: additionalData.userId,
lastNodeExecuted: data?.data.resultData.lastNodeExecuted,
errorNodeType:
data?.data.resultData.error && 'node' in data?.data.resultData.error
? data?.data.resultData.error.node?.type
: undefined,
errorMessage: data?.data.resultData.error?.message.toString(),
});

// subworkflow either finished, or is in status waiting due to a wait node, both cases are considered successes here
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/WorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ export class WorkflowRunner {
success: executionData?.status === 'success',
isManual: data.executionMode === 'manual',
userId: data.userId,
lastNodeExecuted: executionData?.data.resultData.lastNodeExecuted,
errorNodeType:
executionData?.data.resultData.error && 'node' in executionData?.data.resultData.error
? executionData?.data.resultData.error.node?.type
: undefined,
errorMessage: executionData?.data.resultData.error?.message.toString(),
});
if (this.externalHooks.exists('workflow.postExecute')) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { mock } from 'jest-mock-extended';
import { AuditEventRelay } from '../audit-event-relay.service';
import type { MessageEventBus } from '../MessageEventBus/MessageEventBus';
import type { Event } from '../event.types';
import type { EventService } from '../event.service';
import { EventService } from '../event.service';

describe('AuditorService', () => {
const eventBus = mock<MessageEventBus>();
const eventService = mock<EventService>();
const eventService = new EventService();
const auditor = new AuditEventRelay(eventService, eventBus);
auditor.init();

afterEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -80,4 +81,50 @@ describe('AuditorService', () => {
},
});
});

it('should log on `workflow-post-execute` for successful execution', () => {
const payload = {
executionId: 'some-id',
success: true,
userId: 'some-id',
workflowId: 'some-id',
isManual: true,
workflowName: 'some-name',
metadata: {},
lastNodeExecuted: 'some-node',
errorNodeType: 'some-type',
errorMessage: 'some-message',
};

eventService.emit('workflow-post-execute', payload);

const { lastNodeExecuted: _, errorNodeType: __, errorMessage: ___, ...rest } = payload;

expect(eventBus.sendWorkflowEvent).toHaveBeenCalledWith({
eventName: 'n8n.workflow.success',
payload: rest,
});
});

it('should handle `workflow-post-execute` event for unsuccessful execution', () => {
const payload = {
executionId: 'some-id',
success: false,
userId: 'some-id',
workflowId: 'some-id',
isManual: true,
workflowName: 'some-name',
metadata: {},
lastNodeExecuted: 'some-node',
errorNodeType: 'some-type',
errorMessage: 'some-message',
};

eventService.emit('workflow-post-execute', payload);

expect(eventBus.sendWorkflowEvent).toHaveBeenCalledWith({
eventName: 'n8n.workflow.failed',
payload,
});
});
});
33 changes: 30 additions & 3 deletions packages/cli/src/eventbus/audit-event-relay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,37 @@ export class AuditEventRelay {
});
}

private workflowPostExecute(event: Event['workflow-post-execute']) {
private workflowPostExecute({
executionId,
success,
userId,
workflowId,
isManual,
workflowName,
metadata,
lastNodeExecuted,
errorNodeType,
errorMessage,
}: Event['workflow-post-execute']) {
const payload: Event['workflow-post-execute'] = {
ivov marked this conversation as resolved.
Show resolved Hide resolved
executionId,
success,
userId,
workflowId,
isManual,
workflowName,
metadata,
};

if (!success) {
payload.lastNodeExecuted = lastNodeExecuted;
payload.errorNodeType = errorNodeType;
payload.errorMessage = errorMessage;
}

void this.eventBus.sendWorkflowEvent({
eventName: 'n8n.workflow.success',
payload: event,
eventName: success ? 'n8n.workflow.success' : 'n8n.workflow.failed',
payload,
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/eventbus/event.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export type Event = {
isManual: boolean;
workflowName: string;
metadata?: Record<string, string>;

// failed case
lastNodeExecuted?: string;
errorNodeType?: string;
errorMessage?: string;
};

'node-pre-execute': {
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/executions/execution-recovery.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ export class ExecutionRecoveryService {
executionId: execution.id,
success: execution.status === 'success',
isManual: execution.mode === 'manual',
lastNodeExecuted: execution.data.resultData.lastNodeExecuted,
errorNodeType:
execution.data.resultData.error && 'node' in execution.data.resultData.error
? execution.data.resultData.error.node?.type
: undefined,
errorMessage: execution.data.resultData.error?.message.toString(),
});

const externalHooks = getWorkflowHooksMain(
Expand Down