Skip to content

Commit

Permalink
feat(cli): add options.deamon
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 9, 2021
1 parent aef0404 commit 46be100
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
6 changes: 6 additions & 0 deletions packages/koishi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ export interface WatchConfig extends WatchOptions {
fullReload?: boolean
}

interface DeamonConfig {
exitCommand?: boolean | string
autoRestart?: boolean
}

export interface AppConfig extends AppOptions {
plugins?: PluginConfig
logLevel?: LogLevel
logDiff?: boolean
logTime?: string | boolean
watch?: WatchConfig
deamon?: DeamonConfig
}
25 changes: 10 additions & 15 deletions packages/koishi/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ interface WorkerOptions {
'--'?: string[]
}

const codes = [
134, // heap out of memory
114, // preserved for koishi
]

let child: ChildProcess

process.on('SIGINT', () => {
Expand All @@ -27,32 +22,32 @@ process.on('SIGINT', () => {
})

interface Message {
type: 'start' | 'exit'
payload: any
type: 'start' | 'queue'
body: any
}

let payload: any
let buffer: any

function createWorker(options: WorkerOptions) {
child = fork(resolve(__dirname, 'worker'), [], {
execArgv: options['--'],
})

let started = false
let config: { autoRestart: boolean }

child.on('message', (message: Message) => {
if (message.type === 'start') {
started = true
if (payload) {
child.send({ type: 'send', payload })
config = message.body
if (buffer) {
child.send({ type: 'send', body: buffer })
}
} else if (message.type === 'exit') {
payload = message.payload
} else if (message.type === 'queue') {
buffer = message.body
}
})

child.on('exit', (code) => {
if (!started || !codes.includes(code)) {
if (!config || (!config.autoRestart && code !== 114)) {
process.exit(code)
}
createWorker(options)
Expand Down
13 changes: 8 additions & 5 deletions packages/koishi/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ if (process.env.KOISHI_DEBUG) {

interface Message {
type: 'send'
payload: any
body: any
}

process.on('message', (data: Message) => {
if (data.type === 'send') {
const { channelId, sid, message } = data.payload
const { channelId, sid, message } = data.body
const bot = app.bots[sid]
bot.sendMessage(channelId, message)
}
Expand All @@ -138,7 +138,10 @@ if (config.type) {

const app = new App(config)

app.command('exit', '停止机器人运行', { authority: 4 })
const { exitCommand, autoRestart = true } = config.deamon || {}

exitCommand && app
.command('exit', '停止机器人运行', { authority: 4 })
.option('restart', '-r 重新启动')
.shortcut('关机', { prefix: true })
.shortcut('重启', { prefix: true, options: { restart: true } })
Expand All @@ -148,7 +151,7 @@ app.command('exit', '停止机器人运行', { authority: 4 })
await session.send('正在关机……').catch(noop)
process.exit()
}
process.send({ type: 'exit', payload: { channelId, sid, message: '已成功重启。' } })
process.send({ type: 'queue', body: { channelId, sid, message: '已成功重启。' } })
await session.send(`正在重启……`).catch(noop)
process.exit(114)
})
Expand Down Expand Up @@ -224,7 +227,7 @@ app.start().then(() => {
Logger.timestamp = Date.now()
Logger.showDiff = config.logDiff ?? !Logger.showTime

process.send({ type: 'start' })
process.send({ type: 'start', body: { autoRestart } })
createWatcher()
}, handleException)

Expand Down

0 comments on commit 46be100

Please sign in to comment.