diff --git a/packages/cli/src/commands/BaseCommand.ts b/packages/cli/src/commands/BaseCommand.ts index 1ef136b209..65b6dd27fc 100644 --- a/packages/cli/src/commands/BaseCommand.ts +++ b/packages/cli/src/commands/BaseCommand.ts @@ -1,4 +1,5 @@ import { Command } from '@oclif/command'; +import { ExitError } from '@oclif/errors'; import type { INodeTypes } from 'n8n-workflow'; import { LoggerProxy, ErrorReporterProxy as ErrorReporter, sleep } from 'n8n-workflow'; import type { IUserSettings } from 'n8n-core'; @@ -35,8 +36,8 @@ export abstract class BaseCommand extends Command { async init(): Promise { await initErrorHandling(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - this.stopProcess = this.stopProcess.bind(this); + process.once('SIGTERM', async () => this.stopProcess()); + process.once('SIGINT', async () => this.stopProcess()); // Make sure the settings exist this.userSettings = await UserSettings.prepareUserSettings(); @@ -88,7 +89,11 @@ export abstract class BaseCommand extends Command { async finally(error: Error | undefined) { if (inTest || this.id === 'start') return; - if (Db.isInitialized) await Db.connection.destroy(); - this.exit(error ? 1 : 0); + if (Db.isInitialized) { + await sleep(100); // give any in-flight query some time to finish + await Db.connection.destroy(); + } + const exitCode = error instanceof ExitError ? error.oclif.exit : error ? 1 : 0; + this.exit(exitCode); } } diff --git a/packages/cli/src/commands/executeBatch.ts b/packages/cli/src/commands/executeBatch.ts index 592c1873c6..d12c7fabd8 100644 --- a/packages/cli/src/commands/executeBatch.ts +++ b/packages/cli/src/commands/executeBatch.ts @@ -95,7 +95,7 @@ export class ExecuteBatch extends BaseCommand { * Gracefully handles exit. * @param {boolean} skipExit Whether to skip exit or number according to received signal */ - static async stopProcess(skipExit: boolean | number = false) { + async stopProcess(skipExit: boolean | number = false) { if (ExecuteBatch.cancelled) { process.exit(0); } @@ -168,11 +168,6 @@ export class ExecuteBatch extends BaseCommand { } async run() { - // eslint-disable-next-line @typescript-eslint/unbound-method - process.once('SIGTERM', ExecuteBatch.stopProcess); - // eslint-disable-next-line @typescript-eslint/unbound-method - process.once('SIGINT', ExecuteBatch.stopProcess); - // eslint-disable-next-line @typescript-eslint/no-shadow const { flags } = this.parse(ExecuteBatch); @@ -318,7 +313,7 @@ export class ExecuteBatch extends BaseCommand { console.log(this.formatJsonOutput(results)); } - await ExecuteBatch.stopProcess(true); + await this.stopProcess(true); if (results.summary.failedExecutions > 0) { this.exit(1); diff --git a/packages/cli/src/commands/start.ts b/packages/cli/src/commands/start.ts index 28b0927830..c3761f02e7 100644 --- a/packages/cli/src/commands/start.ts +++ b/packages/cli/src/commands/start.ts @@ -1,5 +1,4 @@ /* eslint-disable @typescript-eslint/await-thenable */ -/* eslint-disable @typescript-eslint/unbound-method */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ import path from 'path'; @@ -199,10 +198,6 @@ export class Start extends BaseCommand { } async init() { - // Make sure that n8n shuts down gracefully if possible - process.once('SIGTERM', this.stopProcess); - process.once('SIGINT', this.stopProcess); - await this.initCrashJournal(); await super.init(); this.logger.info('Initializing n8n process'); diff --git a/packages/cli/src/commands/webhook.ts b/packages/cli/src/commands/webhook.ts index e8cafdbfbc..c061efbcbe 100644 --- a/packages/cli/src/commands/webhook.ts +++ b/packages/cli/src/commands/webhook.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/unbound-method */ import { flags } from '@oclif/command'; import { LoggerProxy, sleep } from 'n8n-workflow'; import config from '@/config'; @@ -71,10 +70,6 @@ export class Webhook extends BaseCommand { this.error('Webhook processes can only run with execution mode as queue.'); } - // Make sure that n8n shuts down gracefully if possible - process.once('SIGTERM', this.stopProcess); - process.once('SIGINT', this.stopProcess); - await this.initCrashJournal(); await super.init(); diff --git a/packages/cli/src/commands/worker.ts b/packages/cli/src/commands/worker.ts index 591aa4f836..bd4c09266b 100644 --- a/packages/cli/src/commands/worker.ts +++ b/packages/cli/src/commands/worker.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/unbound-method */ import express from 'express'; import http from 'http'; import type PCancelable from 'p-cancelable'; @@ -219,10 +218,6 @@ export class Worker extends BaseCommand { } async init() { - // Make sure that n8n shuts down gracefully if possible - process.once('SIGTERM', this.stopProcess); - process.once('SIGINT', this.stopProcess); - await this.initCrashJournal(); await super.init(); this.logger.debug('Starting n8n worker...');