-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.js
34 lines (25 loc) · 925 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
'use strict';
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Master process
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('listening', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' listening');
});
cluster.on('disconnect', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' disconnect. Exit should follow.');
});
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' exit (' + (signal||code) + '). Starting replacement...');
cluster.fork();
});
} else {
// Kittens all listen on the same port. Master load balances.
var port = process.env.PORT || 8000;
var app = require('./app')({ logging: false });
app.listen(port);
console.log('Server started on http://localhost:' + port);
}