forked from ischyrus/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster.js
More file actions
40 lines (33 loc) · 960 Bytes
/
Copy pathmaster.js
File metadata and controls
40 lines (33 loc) · 960 Bytes
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
var cluster = require('cluster');
var fs = require('fs');
var crypto = require('crypto');
function getSHA1 () {
return crypto.createHash('sha1')
.update(fs.readFileSync(__filename))
.digest('hex');
}
var version = getSHA1();
console.log('Starting master process with pid ' + process.pid);
//fork the first process
cluster.fork();
process.on('SIGHUP', function () {
if (version !== getSHA1()) {
console.log('master server changed, exiting');
return process.exit(1);
}
console.log('reloading...');
var new_worker = cluster.fork();
new_worker.once('listening', function () {
//stop all other workers
for(var id in cluster.workers) {
if (id === new_worker.id.toString()) {
continue;
}
cluster.workers[id].process.kill('SIGTERM');
}
});
}).on('SIGTERM', function () {
for(var id in cluster.workers) {
cluster.workers[id].process.kill('SIGTERM');
}
});