Skip to content

Commit

Permalink
进程退出时,带着nginx一起结束
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Dec 23, 2021
1 parent 4addd38 commit e003a02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function bootstrap(version: string): Promise<void> {
await cluster.enable()
} catch (e) {
console.error(e)
process.exit(1)
cluster.exit(1)
}
console.log(colors.rainbow(`done, serving ${files.files.length} files`))

Expand Down Expand Up @@ -59,6 +59,7 @@ export async function bootstrap(version: string): Promise<void> {
// 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)
Expand Down
22 changes: 15 additions & 7 deletions src/cluster.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'],
})

Expand Down Expand Up @@ -243,7 +244,7 @@ export class Cluster {
this.isEnabled = true
} catch (e) {
console.error(e)
process.exit(1)
this.exit(1)
}
}

Expand Down Expand Up @@ -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<void> {
return new Bluebird<void>((resolve, reject) => {
this.io.emit('enable', {
Expand All @@ -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')
}
Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit e003a02

Please sign in to comment.