From e003a02c866ff4c2ec766be3804554861e04bd70 Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Thu, 23 Dec 2021 09:57:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E9=80=80=E5=87=BA=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=B8=A6=E7=9D=80nginx=E4=B8=80=E8=B5=B7=E7=BB=93?= =?UTF-8?q?=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bootstrap.ts | 3 ++- src/cluster.ts | 22 +++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/bootstrap.ts b/src/bootstrap.ts index 6b5e07b..910c06a 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -31,7 +31,7 @@ export async function bootstrap(version: string): Promise { await cluster.enable() } catch (e) { console.error(e) - process.exit(1) + cluster.exit(1) } console.log(colors.rainbow(`done, serving ${files.files.length} files`)) @@ -59,6 +59,7 @@ export async function bootstrap(version: string): Promise { // eslint-disable-next-line no-console console.log('unregister success, waiting for background task, ctrl+c again to force kill') server.close() + cluster.nginxProcess?.kill() } process.on('SIGTERM', onStop) process.on('SIGINT', onStop) diff --git a/src/cluster.ts b/src/cluster.ts index c04bdab..fbdeaa5 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -1,5 +1,5 @@ import * as Bluebird from 'bluebird' -import {spawn} from 'child_process' +import {ChildProcess, spawn} from 'child_process' import * as colors from 'colors/safe' import * as express from 'express' import {readFileSync} from 'fs' @@ -36,6 +36,7 @@ export class Cluster { public isEnabled = false public keepAliveInterval: Timeout public interval: Timeout + public nginxProcess: ChildProcess private keepAliveError = 0 private readonly prefixUrl = process.env.CLUSTER_BMCLAPI || 'https://openbmclapi.bangbang93.com' @@ -177,7 +178,7 @@ export class Cluster { const logFd = await open(logFile, 'a') await ftruncate(logFd) - spawn('nginx', ['-c', confFile], { + this.nginxProcess = spawn('nginx', ['-c', confFile], { stdio: [null, logFd, 'inherit'], }) @@ -243,7 +244,7 @@ export class Cluster { this.isEnabled = true } catch (e) { console.error(e) - process.exit(1) + this.exit(1) } } @@ -297,6 +298,13 @@ export class Cluster { await outputFile(join(this.cacheDir, 'key.pem'), cert.key) } + public exit(code: number = 0): never { + if (this.nginxProcess) { + this.nginxProcess.kill() + } + process.exit(code) + } + private async _enable(): Promise { return new Bluebird((resolve, reject) => { this.io.emit('enable', { @@ -318,7 +326,7 @@ export class Cluster { const status = await Bluebird.try(async () => this.keepAlive()).timeout(ms('10s'), 'keep alive timeout') if (!status) { console.log('kicked by server') - process.exit(1) + this.exit(1) } else { console.log('keep alive success') } @@ -338,7 +346,7 @@ export class Cluster { .timeout(ms('30s'), 'restart timeout') .catch((e) => { console.error(e, 'restart failed') - process.exit(1) + this.exit(1) }) } } finally { @@ -350,10 +358,10 @@ export class Cluster { console.error('cannot connect to server', err) if (this.server) { this.server.close(() => { - process.exit(1) + this.exit(1) }) } else { - process.exit(1) + this.exit(1) } }