From 6203496db53773468f8e1dc03575f5c871659187 Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:11:43 +0200 Subject: [PATCH] fix(core): fix shutdown if terminating before hooks are initialized --- packages/cli/src/commands/BaseCommand.ts | 2 +- packages/cli/src/commands/start.ts | 2 +- packages/cli/src/commands/webhook.ts | 2 +- packages/cli/src/commands/worker.ts | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/commands/BaseCommand.ts b/packages/cli/src/commands/BaseCommand.ts index fc8b28c0df4fb..d39d6744e1a6d 100644 --- a/packages/cli/src/commands/BaseCommand.ts +++ b/packages/cli/src/commands/BaseCommand.ts @@ -26,7 +26,7 @@ import { WorkflowHistoryManager } from '@/workflows/workflowHistory/workflowHist export abstract class BaseCommand extends Command { protected logger = Container.get(Logger); - protected externalHooks: IExternalHooksClass; + protected externalHooks?: IExternalHooksClass; protected nodeTypes: NodeTypes; diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index 42a5b463f50a5..b56fcfca90d0b 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -101,7 +101,7 @@ export class Start extends BaseCommand { // Stop with trying to activate workflows that could not be activated this.activeWorkflowRunner.removeAllQueuedWorkflowActivations(); - await this.externalHooks.run('n8n.stop', []); + await this.externalHooks?.run('n8n.stop', []); setTimeout(async () => { // In case that something goes wrong with shutdown we diff --git a/packages/cli/src/commands/webhook.ts b/packages/cli/src/commands/webhook.ts index 12f08ab690b2f..49bda1d53e3b1 100644 --- a/packages/cli/src/commands/webhook.ts +++ b/packages/cli/src/commands/webhook.ts @@ -39,7 +39,7 @@ export class Webhook extends BaseCommand { this.logger.info('\nStopping n8n...'); try { - await this.externalHooks.run('n8n.stop', []); + await this.externalHooks?.run('n8n.stop', []); setTimeout(async () => { // In case that something goes wrong with shutdown we diff --git a/packages/cli/src/commands/worker.ts b/packages/cli/src/commands/worker.ts index 586a5079eab97..06ae3623017a5 100644 --- a/packages/cli/src/commands/worker.ts +++ b/packages/cli/src/commands/worker.ts @@ -79,7 +79,7 @@ export class Worker extends BaseCommand { await Worker.jobQueue.pause(true); try { - await this.externalHooks.run('n8n.stop', []); + await this.externalHooks?.run('n8n.stop', []); const maxStopTime = config.getEnv('queue.bull.gracefulShutdownTimeout') * 1000; @@ -483,7 +483,7 @@ export class Worker extends BaseCommand { }); await new Promise((resolve) => server.listen(port, () => resolve())); - await this.externalHooks.run('worker.ready'); + await this.externalHooks?.run('worker.ready'); this.logger.info(`\nn8n worker health check via, port ${port}`); }