-
Notifications
You must be signed in to change notification settings - Fork 1
/
server-cluster.js
41 lines (31 loc) · 1.11 KB
/
server-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
var cluster = require('cluster');
var job = (process.argv[2] == undefined || process.argv[2] == 'scrape') ? 'scrape' : 'archive';
function startWorker() {
var worker = cluster.fork();
console.log('SERVER-CLUSTER_OPERATION: Starting worker %d of 1.', worker.id);
}
if(cluster.isMaster){
// we're only running one worker at a time
[0].forEach(function(){
startWorker();
});
// log any workers that disconnect; if a worker disconnects, it
// should then exit, so we'll wait for the exit event to spawn
// a new worker to replace it
cluster.on('disconnect', function(worker){
console.log('SERVER-CLUSTER_OPERATION: Worker %d disconnected from the cluster.',
worker.id);
});
// when a worker dies (exits), create a worker to replace it
cluster.on('exit', function(worker, code, signal){
console.log('SERVER-CLUSTER_OPERATION: Worker %d died with exit code %d (%s)',
worker.id, code, signal);
// only start a new worked if in scrape mode
if (job == 'scrape') {
startWorker();
}
});
} else {
// start our app on worker; see server.js
require('./server.js')();
}