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): Filter out workflows that failed to activate on startup #6676

Merged
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
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"name": "Launch n8n with debug",
"program": "${workspaceFolder}/packages/cli/bin/n8n",
"cwd": "${workspaceFolder}/packages/cli/bin",
// "args": ["start", "--tunnel"],
"request": "launch",
"skipFiles": ["<node_internals>/**"],
"type": "node",
Expand Down
16 changes: 11 additions & 5 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class ActiveWorkflowRunner {
} catch (error) {
ErrorReporter.error(error);
Logger.info(
' => ERROR: Workflow could not be activated on first try, keep on trying',
' => ERROR: Workflow could not be activated on first try, keep on trying if not an auth issue',
);
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Logger.info(` ${error.message}`);
Expand All @@ -148,8 +148,10 @@ export class ActiveWorkflowRunner {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
this.executeErrorWorkflow(error, workflowData, 'internal');

// Keep on trying to activate the workflow
this.addQueuedWorkflowActivation('init', workflowData);
if (!error.message.includes('Authorization')) {
// Keep on trying to activate the workflow if not an auth issue
this.addQueuedWorkflowActivation('init', workflowData);
}
}
}
Logger.verbose('Finished initializing active workflows (startup)');
Expand Down Expand Up @@ -350,7 +352,9 @@ export class ActiveWorkflowRunner {
select: ['id'],
where: { active: true },
});
return activeWorkflows.map((workflow) => workflow.id.toString());
return activeWorkflows
.map((workflow) => workflow.id)
.filter((workflowId) => !this.activationErrors[workflowId]);
} else {
const active = await Db.collections.Workflow.find({
select: ['id'],
Expand All @@ -366,7 +370,9 @@ export class ActiveWorkflowRunner {
select: ['workflowId'],
where,
});
return shared.map((id) => id.workflowId.toString());
return shared
.map((id) => id.workflowId)
.filter((workflowId) => !this.activationErrors[workflowId]);
}
}

Expand Down
37 changes: 34 additions & 3 deletions packages/cli/test/unit/ActiveWorkflowRunner.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { v4 as uuid } from 'uuid';
import { mocked } from 'jest-mock';

import type { ICredentialTypes, INodesAndCredentials } from 'n8n-workflow';
import { LoggerProxy, NodeOperationError, Workflow } from 'n8n-workflow';
import type { ICredentialTypes, INode, INodesAndCredentials } from 'n8n-workflow';
import { LoggerProxy, NodeApiError, NodeOperationError, Workflow } from 'n8n-workflow';

import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import * as Db from '@/Db';
Expand Down Expand Up @@ -51,7 +51,7 @@ const generateWorkflows = (count: number): WorkflowEntity[] => {
for (let i = 0; i < count; i++) {
const workflow = new WorkflowEntity();
Object.assign(workflow, {
id: i + 1,
id: (i + 1).toString(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this fix. I've had this change in a branch for weeks, but haven't had the opportunity to sneak it in a PR 🙏🏽

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general, I'd really like us to move away from using Object.assign for creating entities, and use repository.create instead, as that's type-safe.

name: randomName(),
active: true,
createdAt: new Date(),
Expand Down Expand Up @@ -260,4 +260,35 @@ describe('ActiveWorkflowRunner', () => {
activeWorkflowRunner.executeErrorWorkflow(error, workflowData, 'trigger');
expect(workflowExecuteAdditionalDataExecuteErrorWorkflowSpy).toHaveBeenCalledTimes(1);
});

describe('init()', () => {
it('should execute error workflow on failure to activate due to 401', async () => {
databaseActiveWorkflowsCount = 1;

jest.spyOn(ActiveWorkflowRunner.prototype, 'add').mockImplementation(() => {
throw new NodeApiError(
{
id: 'a75dcd1b-9fed-4643-90bd-75933d67936c',
name: 'Github Trigger',
type: 'n8n-nodes-base.githubTrigger',
typeVersion: 1,
position: [0, 0],
} as INode,
{
httpCode: '401',
message: 'Authorization failed - please check your credentials',
},
);
});

const executeSpy = jest.spyOn(ActiveWorkflowRunner.prototype, 'executeErrorWorkflow');

await activeWorkflowRunner.init();

const [error, workflow] = executeSpy.mock.calls[0];

expect(error.message).toContain('Authorization');
expect(workflow.id).toBe('1');
});
});
});