Skip to content

Commit

Permalink
feat(cli): support exit event
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 10, 2021
1 parent 8880c86 commit 3ca3f72
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
6 changes: 6 additions & 0 deletions packages/koishi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export * from 'koishi-core'

export type PluginConfig = Record<string, any> | (string | [string, any?])[]

declare module 'koishi-core' {
interface EventMap {
'exit'(signal: NodeJS.Signals): Promise<void>
}
}

interface LogLevelConfig {
// a little different from koishi-utils
// we don't enforce user to provide a base here
Expand Down
11 changes: 10 additions & 1 deletion packages/koishi/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,17 @@ function createWorker(options: WorkerOptions) {
}
})

/**
* https://tldp.org/LDP/abs/html/exitcodes.html
* - 0: exit manually
* - 130: SIGINT
* - 137: SIGKILL
* - **114: exit and restart (Koishi)**
*/
const closingCode = [0, 130, 137]

child.on('exit', (code) => {
if (!config || (!config.autoRestart && code !== 114)) {
if (!config || closingCode.includes(code) || code !== 114 && !config.autoRestart) {
process.exit(code)
}
createWorker(options)
Expand Down
8 changes: 8 additions & 0 deletions packages/koishi/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ const app = new App(config)

const { exitCommand, autoRestart = true } = config.deamon || {}

const handleSignal = (signal: NodeJS.Signals) => {
new Logger('app').info(`terminated by ${signal}`)
app.parallel('exit', signal).finally(() => process.exit())
}

process.on('SIGINT', handleSignal)
process.on('SIGTERM', handleSignal)

template.set('deamon', {
exiting: '正在关机……',
restarting: '正在重启……',
Expand Down
21 changes: 3 additions & 18 deletions packages/plugin-webui/src/stats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Context, Channel, noop, Session, Logger, Bot, Platform, Time } from 'koishi-core'
import { Context, Channel, noop, Session, Bot, Platform, Time } from 'koishi-core'
import { DataSource } from './data'
import {} from 'koishi'

export type StatRecord = Record<string, number>

Expand Down Expand Up @@ -83,22 +84,7 @@ export class Statistics implements DataSource<Statistics.Payload> {
average = average

constructor(private ctx: Context, public config: Statistics.Config = {}) {
if (config.handleSignals !== false) {
const handleSignal = (signal: NodeJS.Signals) => {
new Logger('app').info(`terminated by ${signal}`)
this.upload(true).finally(() => process.exit())
}

ctx.on('connect', () => {
process.on('SIGINT', handleSignal)
process.on('SIGTERM', handleSignal)
})

ctx.before('disconnect', async () => {
process.off('SIGINT', handleSignal)
process.off('SIGTERM', handleSignal)
})
}
ctx.on('exit', () => this.upload(true))

ctx.on('delegate/database', () => {
this.sync = ctx.database.createSynchronizer()
Expand Down Expand Up @@ -254,7 +240,6 @@ export namespace Statistics {
}

export interface Config {
handleSignals?: boolean
statsInternal?: number
}

Expand Down

0 comments on commit 3ca3f72

Please sign in to comment.