From 42c79cd6f1e495e60a9f038403d9a8a761318f52 Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Tue, 23 May 2023 13:01:50 +0200 Subject: [PATCH] fix: Initialize license in queue mode correctly (#6301) --- packages/cli/src/License.ts | 1 + packages/cli/src/commands/BaseCommand.ts | 23 +++++++++++++++++++++++ packages/cli/src/commands/start.ts | 23 ----------------------- packages/cli/src/commands/webhook.ts | 1 + packages/cli/src/commands/worker.ts | 1 + 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/cli/src/License.ts b/packages/cli/src/License.ts index 9a4597f92a..4d252d4ad6 100644 --- a/packages/cli/src/License.ts +++ b/packages/cli/src/License.ts @@ -98,6 +98,7 @@ export class License { isFeatureEnabled(feature: string): boolean { if (!this.manager) { + getLogger().warn('License manager not initialized'); return false; } diff --git a/packages/cli/src/commands/BaseCommand.ts b/packages/cli/src/commands/BaseCommand.ts index 2368918a57..b8d8c5a322 100644 --- a/packages/cli/src/commands/BaseCommand.ts +++ b/packages/cli/src/commands/BaseCommand.ts @@ -19,6 +19,7 @@ import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials'; import type { IExternalHooksClass } from '@/Interfaces'; import { InternalHooks } from '@/InternalHooks'; import { PostHogClient } from '@/posthog'; +import { License } from '@/License'; export const UM_FIX_INSTRUCTION = 'Please fix the database by running ./packages/cli/bin/n8n user-management:reset'; @@ -119,6 +120,28 @@ export abstract class BaseCommand extends Command { await this.externalHooks.init(); } + async initLicense(): Promise { + const license = Container.get(License); + await license.init(this.instanceId); + + const activationKey = config.getEnv('license.activationKey'); + + if (activationKey) { + const hasCert = (await license.loadCertStr()).length > 0; + + if (hasCert) { + return LoggerProxy.debug('Skipping license activation'); + } + + try { + LoggerProxy.debug('Attempting license activation'); + await license.activate(activationKey); + } catch (e) { + LoggerProxy.error('Could not activate license', e as Error); + } + } + } + async finally(error: Error | undefined) { if (inTest || this.id === 'start') return; if (Db.connectionState.connected) { diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index 016fc48a9e..82640335cd 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -28,7 +28,6 @@ import { EDITOR_UI_DIST_DIR, GENERATED_STATIC_DIR } from '@/constants'; import { eventBus } from '@/eventbus'; import { BaseCommand } from './BaseCommand'; import { InternalHooks } from '@/InternalHooks'; -import { License } from '@/License'; // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires const open = require('open'); @@ -186,28 +185,6 @@ export class Start extends BaseCommand { await Promise.all(files.map(compileFile)); } - async initLicense(): Promise { - const license = Container.get(License); - await license.init(this.instanceId); - - const activationKey = config.getEnv('license.activationKey'); - - if (activationKey) { - const hasCert = (await license.loadCertStr()).length > 0; - - if (hasCert) { - return LoggerProxy.debug('Skipping license activation'); - } - - try { - LoggerProxy.debug('Attempting license activation'); - await license.activate(activationKey); - } catch (e) { - LoggerProxy.error('Could not activate license', e as Error); - } - } - } - async init() { await this.initCrashJournal(); diff --git a/packages/cli/src/commands/webhook.ts b/packages/cli/src/commands/webhook.ts index da2fc44315..a06ed770a9 100644 --- a/packages/cli/src/commands/webhook.ts +++ b/packages/cli/src/commands/webhook.ts @@ -77,6 +77,7 @@ export class Webhook extends BaseCommand { await this.initCrashJournal(); await super.init(); + await this.initLicense(); await this.initBinaryManager(); await this.initExternalHooks(); } diff --git a/packages/cli/src/commands/worker.ts b/packages/cli/src/commands/worker.ts index 48d2269d0e..5463eca9f2 100644 --- a/packages/cli/src/commands/worker.ts +++ b/packages/cli/src/commands/worker.ts @@ -224,6 +224,7 @@ export class Worker extends BaseCommand { await super.init(); this.logger.debug('Starting n8n worker...'); + await this.initLicense(); await this.initBinaryManager(); await this.initExternalHooks(); }