发生未捕获的异常时重新启动Node.js应用程序 [英] Restart Node.js application when uncaught exception occurs
本文介绍了发生未捕获的异常时重新启动Node.js应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果发生异常,我怎么能重启我的应用程序?
How would I be able to restart my app when an exception occurs?
process.on('uncaughtException', function(err) {
// restart app here
});
推荐答案
您可以将该流程作为另一个流程的分支运行,所以如果它死了就可以分叉。您可以使用原生群集模块:
You could run the process as a fork of another process, so you can fork it if it dies. You would use the native Cluster module for this:
var cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
cluster.on('exit', function(worker, code, signal) {
cluster.fork();
});
}
if (cluster.isWorker) {
// put your code here
}
此代码生成一个工作进程,如果在工作进程中抛出错误,它将关闭,退出将重新生成另一个工作进程。
This code spawns one worker process, and if an error is thrown in the worker process, it will close, and the exit will respawn another worker process.
这篇关于发生未捕获的异常时重新启动Node.js应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文