-
Notifications
You must be signed in to change notification settings - Fork 71
/
cluster.js
35 lines (31 loc) · 839 Bytes
/
cluster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
if (!module.parent) process.on('uncaughtException', function(err, next) {
var msg;
if (err instanceof Error) {
msg = '[err]: ' + err + '\n' + err.stack;
} else {
msg = (err.name || err.reason || err.message);
console.error(err);
}
console.error(msg);
next();
});
var cluster = require('cluster');
var util = require('util');
var app = require(__dirname + '/app.js');
var central = require(__dirname + '/lib/central.js');
var numCPUs = Math.min(central.conf.worker, require('os').cpus().length);
function startWorker() {
var worker = cluster.fork();
}
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
startWorker();
}
cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died. restart...');
startWorker();
});
} else {
app.boot();
}