-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cluster.js
40 lines (28 loc) · 909 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
36
37
38
39
/*
* Runs a cluster of webserver processes
*
*/
const cluster = require("cluster");
const Service = require("./Federator");
const CONFIG = require("./Config");
const numWorkers = Math.min(require("os").cpus().length, CONFIG["MAXIMUM_NUMBER_OF_WORKERS"]);
// Start the cluster
if(cluster.isMaster) {
// Create N forks
for(var i = 0; i < numWorkers; i++) {
cluster.fork();
}
// Worker has died, restart if required
cluster.on("exit", function(worker, code, signal) {
if(CONFIG.RESPAWN) {
console.log("[Worker " + worker.process.pid + "] killed with code: " + code + ", and signal: " + signal + ". Respawning.");
cluster.fork();
}
});
} else {
// Create the Services
// Wrap in closure to pass worker.id
new Service(function() {
console.log("[Worker " + cluster.worker.id + "] Service has been started on " + CONFIG.HOST + ":" + CONFIG.PORT);
});
}