-
Notifications
You must be signed in to change notification settings - Fork 11
/
app-cluster.js
68 lines (61 loc) · 1.55 KB
/
app-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
var util = require('util');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var logger = require('./lib/logger');
function createWorker() {
var worker = cluster.fork();
logger.info(util.format('Worker %d forked', worker.process.pid));
}
for(var i = 0; i < numCPUs; i++) {
createWorker();
}
cluster.on('exit', function (worker, code, signal) {
logger.alert(util.format('Worker %d died (%s).', worker.process.pid, signal || code));
switch(code) {
case 1:
logger.alert('Restarting...');
createWorker();
break;
}
});
var signals = [
'SIGHUP',
'SIGINT',
'SIGQUIT',
'SIGILL',
'SIGTRAP',
'SIGABRT',
'SIGBUS',
'SIGFPE',
/**
* Removed 'SIGKILL' from the list.
* Cannot have a listener installed, it will unconditionally terminate node on all platforms.
* @see http://nodejs.org/api/process.html#process_signal_events
*/
'',
'SIGUSR1',
'SIGSEGV',
'SIGUSR2',
/**
* Removed 'SIGPIPE' from the list.
* Issue is the haproxy request results in a SIGPIPE as it closes the connection.
* @see https://bugzilla.redhat.com/show_bug.cgi?id=852598
*/
'',
'SIGALRM',
'SIGTERM'
];
signals.forEach(function (signal, index) {
if(signal) {
process.on(signal, function () {
logger.alert(util.format('Received %s – terminating process', signal));
cluster.disconnect(function () {
process.exit(128 + index + 1);
});
});
}
});
process.on('exit', function (code) {
logger.alert(util.format('Node process stopped (%d)', code));
});