domain-middleware
Version:
uncaughtException middleware for connect, base on `domain` module.
46 lines (37 loc) • 1.25 kB
JavaScript
/*!
* domain-middleware - example/connect_with_cluster/dispatch.js
* Copyright(c) 2013 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
;
/**
* Module dependencies.
*/
// http://nodejs.org/docs/latest/api/domain.html#domain_warning_don_t_ignore_errors
var cluster = require('cluster');
var path = require('path');
cluster.setupMaster({
exec: path.join(__dirname, 'worker.js')
});
// In real life, you'd probably use more than just 2 workers,
// and perhaps not put the master and worker in the same file.
//
// You can also of course get a bit fancier about logging, and
// implement whatever custom logic you need to prevent DoS
// attacks and other bad behavior.
//
// See the options in the cluster documentation.
//
// The important thing is that the master does very little,
// increasing our resilience to unexpected errors.
cluster.fork();
cluster.fork();
cluster.on('disconnect', function (worker) {
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
new Date(), process.pid, worker.process.pid, w.process.pid);
});
cluster.on('exit', function (worker) {
console.error('[%s] [master:%s] wroker:%s exit!',
new Date(), process.pid, worker.process.pid);
});