forked from suprnurd/ciquidus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster
37 lines (33 loc) · 968 Bytes
/
cluster
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
36
37
var cluster = require('cluster');
var fs = require('fs');
if (cluster.isMaster) {
fs.writeFile('./tmp/cluster.pid', process.pid, function (err) {
if (err) {
console.log('Error: unable to create cluster.pid');
process.exit(1);
} else {
console.log('Starting cluster with pid: ' + process.pid);
//ensure workers exit cleanly
process.on('SIGINT', function() {
console.log('Cluster shutting down..');
for (var id in cluster.workers) {
cluster.workers[id].kill();
}
// exit the master process
process.exit(0);
});
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function () {
cluster.fork();
});
}
});
} else {
require('./instance');
}