考虑以下情况,当 nodejs 运行时崩溃或被强制关闭时,我想做一些清理工作,例如取消注册 consul,它是一个服务注册表和发现实用程序。
["exit", "SIGINT", "SIGHUP", "SIGTERM", "SIGQUIT", "SIGUSR1", "SIGUSR2", "uncaughtException"].forEach(function(value){
process.on(value, function(){
consul.agent.check.deregister(env.CONSUL_ID, function(error) {
if (error) throw error;
console.log("DeRegistered Consul.");
process.exit(1);
});
})
});
当我取下容器(docker-compose down)时,信号不会触发,但是,当我在终端中运行 nodejs 实例并按 Ctrl-C 强制退出时它们会触发。
还请检查信号案例,例如我可能不需要未捕获的异常,取消注册应该只在整个 nodejs 实例关闭时发生。这里还有其他不必要的信号或我可能遗漏的信号吗?
编辑
如何启动容器。(使用docker-compose)
dept:
image: node:10
container_name: dept
working_dir: /service
command: bash -c "npm install && npm install nodemon -g && nodemon --exec npm start"
ports:
- 3003:3000
- 9232:9229
volumes:
- "./dept:/dept"