-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster.js
executable file
·44 lines (44 loc) · 1.41 KB
/
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
40
41
42
43
44
var cluster = require('cluster');
var util = require('util');
var config = require('./config');
var logger = require('./utils/logger');
var workerCount = config.get('cluster.workerCount');
cluster.setupMaster({ exec: 'API_App.js' });
function numWorkers() { return Object.keys(cluster.workers).length; }
var stopping = false;
function forkNewWorkers() {
if (!stopping) {
for (var i = numWorkers(); i < workerCount; i++) { cluster.fork(); }
}
}
var workersToStop = [];
function stopWorker(worker) {
logger.info(util.format('stopping worker pid:%s', worker.process.pid));
worker.disconnect();
var killTimer = setTimeout(function () {
worker.kill();
}, 60000);
killTimer.unref();
}
function stopNextWorker() {
var i = workersToStop.pop();
var worker = cluster.workers[i];
if (worker) stopWorker(worker);
}
function stopAllWorkers() {
stopping = true;
logger.info('stopping all workers');
for (var id in cluster.workers) {
stopWorker(cluster.workers[id]);
}
}
cluster.on('listening', stopNextWorker);
cluster.on('disconnect', forkNewWorkers);
process.on('SIGHUP', function () {
logger.info('restarting all workers');
workersToStop = Object.keys(cluster.workers);
stopNextWorker();
});
process.on('SIGTERM', stopAllWorkers);
forkNewWorkers();
logger.info(util.format('api master started with pid:%s', process.pid));