使用集群模块处理SIGTERM
您好,在哪里可以找到使用群集模块时信号处理的源代码?我想处理一个SIGTERM,以便我可以“优雅”关闭http服务器,但是一旦主进程收到此信号,http服务器就会停止侦听。
“终止”处理程序(当SIGTERM或SIGINT时调用此函数)
function handleTermination(httpServer?: http.Server) {
if (Store.receive<symbol, boolean>(STORE_KEYS.isTerminating)) {
return
}
Store.store<symbol, boolean>(STORE_KEYS.isTerminating, true)
if (httpServer) {
if (!httpServer.listening || isDevelopment) {
closeAll()
process.exit(0)
return
}
setTimeout(() => {
if (process.env.GRACEFUL_SHUTDOWN !== undefined) {
gracefulCloseSockets()
}
httpServer.close(endProcess)
}, 5000)
} else {
endProcess()
}
}
示例路由器处理程
function timerHandler(_req: RequestObject, respond: ResponseHandler) {
setTimeout(() => respond('ok'), 2000)
}
回答如下:
process.on(“exit”,cb)或process.on(“beforeExit”,cb)怎么样?