-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.js
42 lines (31 loc) · 889 Bytes
/
master.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
///////////////////////
// SIGHUP Management //
///////////////////////
'use strict';
var cluster = require('cluster');
console.log('Master started with PID ' + process.pid);
var globalPwd;
// Forks the first process
var firstWorker = cluster.fork();
firstWorker.on('message', function (msg) {
globalPwd = msg;
});
firstWorker.send(false);
process.on('SIGHUP', function () {
console.log('Reloading...');
var newWorker = cluster.fork();
newWorker.once('listening', function () {
// Stops all other workers
for(var id in cluster.workers) {
if (cluster.workers.hasOwnProperty(id)) {
if (id === newWorker.id.toString()) {
continue;
}
cluster.workers[id].kill('SIGTERM');
}
}
});
if (globalPwd) {
newWorker.send(globalPwd);
}
});