Skip to content

Commit

Permalink
feat(cli): handle signals when exit
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 12, 2023
1 parent 79eadab commit 51b9251
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"shx": "^0.3.4",
"source-map-support": "^0.5.21",
"typescript": "^5.3.2",
"yakumo": "^1.0.0-alpha.1",
"yakumo": "^1.0.0-alpha.3",
"yakumo-esbuild": "^1.0.0-alpha.0",
"yakumo-esbuild-yaml": "^1.0.0-alpha.0",
"yakumo-mocha": "^1.0.0-alpha.0",
Expand Down
28 changes: 22 additions & 6 deletions packages/koishi/src/cli/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,29 @@ function createWorker(options: Dict<any>) {
}
})

function shouldExit(code: number) {
// https://nodejs.org/api/process.html#signal-events
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/signal
const signals: NodeJS.Signals[] = [
'SIGABRT',
'SIGBREAK',
'SIGBUS',
'SIGFPE',
'SIGHUP',
'SIGILL',
'SIGINT',
'SIGKILL',
'SIGSEGV',
'SIGSTOP',
'SIGTERM',
]

function shouldExit(code: number, signal: NodeJS.Signals) {
// start failed
if (!config) return true

// exit manually or by signal
// https://tldp.org/LDP/abs/html/exitcodes.html
if (code === 0 || code >= 128 && code < 128 + 16) return true
// exit manually
if (code === 0) return true
if (signals.includes(signal)) return true

// restart manually
if (code === 51) return false
Expand All @@ -86,8 +102,8 @@ function createWorker(options: Dict<any>) {
return !config.autoRestart
}

child.on('exit', (code) => {
if (shouldExit(code)) {
child.on('exit', (code, signal) => {
if (shouldExit(code, signal)) {
process.exit(code)
}
createWorker(options)
Expand Down

0 comments on commit 51b9251

Please sign in to comment.