-
Notifications
You must be signed in to change notification settings - Fork 5
/
boot.js
67 lines (55 loc) · 1.79 KB
/
boot.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var cluster = require('cluster');
var numCpus = require('os').cpus().length;
cluster.setupMaster({exec: __dirname + '/server.js'});
function workerIds() { return Object.keys(cluster.workers); }
function numWorkers() { return workerIds().length; }
var stopping = false;
//Forks workers unless the server is stopping
function forkNewWorkers() {
if (!stopping) {
for (var i = numWorkers(); i < numCpus; i++) {
cluster.fork();
}
}
}
var workersToStop = [];
//Stops single workers and waits 60 seconds after disconnect before SIGTERM
function stopWorker(worker) {
console.log('stopping', worker.process.pid);
worker.disconnect();
var killTimer = setTimeout(function () {
worker.kill();
}, 60000);
killTimer.unref();
}
//Next worker queued to restart will disconnect
//This allows the process 60 seconds to finish before sending SIGTERM
function stopNextWorker() {
var i = workersToStop.pop();
var worker = cluster.workers[i];
if (worker) stopWorker(worker);
}
//Stops all workers at once
function stopAllWorkers() {
stopping = true;
console.log('stopping all workers');
workerIds().forEach(function (id) {
stopWorker(cluster.workers[id]);
});
}
//Worker is now listening to a port
//Once ready, the next worker can restart
cluster.on('listening', stopNextWorker);
//A worker has disconnected (killed or by processing workersToStop)
cluster.on('disconnect', forkNewWorkers);
//HUP signal sent to master that all workers need to restart sequentially
process.on('SIGHUP', function() {
console.log('restarting all workers');
workersToStop = workerIds();
stopNextWorker();
});
//Kill all workers at once when a terminate signal is received
process.on('SIGTERM', stopAllWorkers);
//Fork off initial workers
forkNewWorkers();
console.log('app master', process.pid, 'booted');