From fdb2c18ecc49d1e8759e809d2e2c2e4aa17009da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 23 Nov 2023 12:18:39 +0100 Subject: [PATCH] fix(core): Tighten checks for multi-main setup usage (#7788) https://n8nio.slack.com/archives/C05HRPLSGTT/p1700731476321999?thread_ts=1700729359.746899&cid=C05HRPLSGTT --- packages/cli/src/ActiveWorkflowRunner.ts | 5 +---- packages/cli/src/License.ts | 4 +--- packages/cli/src/commands/start.ts | 2 +- .../orchestration/main/MultiMainSetup.ee.ts | 3 ++- packages/cli/src/services/pruning.service.ts | 1 + .../cli/src/workflows/workflows.services.ts | 20 +++++++++---------- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/packages/cli/src/ActiveWorkflowRunner.ts b/packages/cli/src/ActiveWorkflowRunner.ts index de684c9d2c..5c2807d769 100644 --- a/packages/cli/src/ActiveWorkflowRunner.ts +++ b/packages/cli/src/ActiveWorkflowRunner.ts @@ -4,7 +4,6 @@ import { Service } from 'typedi'; import { ActiveWorkflows, NodeExecuteFunctions } from 'n8n-core'; -import config from '@/config'; import type { ExecutionError, @@ -99,9 +98,7 @@ export class ActiveWorkflowRunner implements IWebhookManager { ) {} async init() { - if (config.getEnv('executions.mode') === 'queue' && config.getEnv('multiMainSetup.enabled')) { - await this.multiMainSetup.init(); - } + await this.multiMainSetup.init(); await this.addActiveWorkflows('init'); diff --git a/packages/cli/src/License.ts b/packages/cli/src/License.ts index 596695fb46..fa9f6ca32d 100644 --- a/packages/cli/src/License.ts +++ b/packages/cli/src/License.ts @@ -51,9 +51,7 @@ export class License { return; } - if (config.getEnv('executions.mode') === 'queue' && config.getEnv('multiMainSetup.enabled')) { - await this.multiMainSetup.init(); - } + await this.multiMainSetup.init(); const isMainInstance = instanceType === 'main'; const server = config.getEnv('license.serverUrl'); diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index 3d8b98e28c..40088c52b1 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -117,7 +117,7 @@ export class Start extends BaseCommand { this.pruningService.stopPruning(); } - if (config.getEnv('executions.mode') === 'queue' && config.getEnv('multiMainSetup.enabled')) { + if (Container.get(MultiMainSetup).isEnabled) { await this.activeWorkflowRunner.removeAllTriggerAndPollerBasedWorkflows(); await Container.get(MultiMainSetup).shutdown(); diff --git a/packages/cli/src/services/orchestration/main/MultiMainSetup.ee.ts b/packages/cli/src/services/orchestration/main/MultiMainSetup.ee.ts index 40a4a18da8..80987078eb 100644 --- a/packages/cli/src/services/orchestration/main/MultiMainSetup.ee.ts +++ b/packages/cli/src/services/orchestration/main/MultiMainSetup.ee.ts @@ -14,6 +14,7 @@ export class MultiMainSetup extends SingleMainSetup { return ( config.getEnv('executions.mode') === 'queue' && config.getEnv('multiMainSetup.enabled') && + config.getEnv('generic.instanceType') === 'main' && this.isLicensed ); } @@ -37,7 +38,7 @@ export class MultiMainSetup extends SingleMainSetup { private leaderCheckInterval: NodeJS.Timer | undefined; async init() { - if (this.isInitialized) return; + if (!this.isEnabled || this.isInitialized) return; await this.initPublisher(); diff --git a/packages/cli/src/services/pruning.service.ts b/packages/cli/src/services/pruning.service.ts index 4d1060f639..706b7a2454 100644 --- a/packages/cli/src/services/pruning.service.ts +++ b/packages/cli/src/services/pruning.service.ts @@ -40,6 +40,7 @@ export class PruningService { if ( config.getEnv('multiMainSetup.enabled') && + config.getEnv('generic.instanceType') === 'main' && config.getEnv('multiMainSetup.instanceType') === 'follower' ) { return false; diff --git a/packages/cli/src/workflows/workflows.services.ts b/packages/cli/src/workflows/workflows.services.ts index b41ff747fa..162ad2aaf5 100644 --- a/packages/cli/src/workflows/workflows.services.ts +++ b/packages/cli/src/workflows/workflows.services.ts @@ -372,19 +372,17 @@ export class WorkflowsService { } } - if (config.getEnv('executions.mode') === 'queue' && config.getEnv('multiMainSetup.enabled')) { - const multiMainSetup = Container.get(MultiMainSetup); + const multiMainSetup = Container.get(MultiMainSetup); - await multiMainSetup.init(); + await multiMainSetup.init(); - if (multiMainSetup.isEnabled) { - await Container.get(MultiMainSetup).broadcastWorkflowActiveStateChanged({ - workflowId, - oldState, - newState: updatedWorkflow.active, - versionId: shared.workflow.versionId, - }); - } + if (multiMainSetup.isEnabled) { + await Container.get(MultiMainSetup).broadcastWorkflowActiveStateChanged({ + workflowId, + oldState, + newState: updatedWorkflow.active, + versionId: shared.workflow.versionId, + }); } return updatedWorkflow;