fix(core): Stop using unbound calls to stopProcess (no-changelog) (#5456)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-02-10 16:34:39 +01:00 committed by GitHub
parent d266401ed8
commit 9a331ec7b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 26 deletions

View file

@ -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<void> {
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);
}
}

View file

@ -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);

View file

@ -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');

View file

@ -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();

View file

@ -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...');