Gracefuly在Nodejs中处理exception

我正在使用nodejs为Web服务构build脱机caching系统。 基本上,它偶尔轮询Web服务的公共方法,并将数据caching在memcached中。 工作得很好。

但是,我希望它是防弹的,并能在任何未捕获的exception情况下生存下来,从而降低服务器的性能。 我已经阅读了各种不同的方法,我相信集群模块非常适合我的需要。 但是,我不会为了我的目的而使用所有的CPU核心,因为我只有1个线程正在运行并更新caching。

我将会使用它的唯一原因是为了优雅地杀死工人并轻松地换一个新工人

var memwatch = require('memwatch'), cluster = require('cluster'); if (cluster.isMaster) { console.log('start cluster with 1 workers'); cluster.fork(); cluster.on('exit', function(worker) { console.log('worker %s died. restart...', worker.process.pid); cluster.fork(); }); } else { var http = require('http'), app = require("./app.js"); http.createServer().listen(9000); app.init(); } process.on('uncaughtException', function (err) { console.error((new Date).toUTCString() + ' uncaughtException:', err.message) console.error(err.stack) process.exit(1) }) memwatch.on("leak", function(){ console.log("leak detected"); }); 

你会认为这是一个正确的方法来解决我的问题吗?

谢谢

如果你只运行一个工人,那么使用foreverpm2这样的主pipe人员更容易,他们是专门为此工作而编写的。 如果你打算增加更多的工人并使用集群负载均衡,那么是的,这无疑是一个正确的方法。