-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.js
130 lines (98 loc) · 2.94 KB
/
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Run this in production:
// forever start master.js
var cluster = require('cluster'),
numCPUs = require('os').cpus().length,
numWorkers = Math.min(2, numCPUs),
expectedWorkers = numWorkers,
fs = require('fs');
if( !cluster.isMaster ) return;
cluster.setupMaster({
exec:'app.js',
silent:true
});
for( var i=0; i<numWorkers; i++ ) {
cluster.fork();
}
/** Inside a worker
* process.send('sent to master', {my:'data'});
*/
/** In master
* worker.on('message', function(data) {
* var otherWorker;
* for( var i in cluster.workers ) {
* otherWorker = cluster.workers[i];
* if( otherWorker !== worker ) {
* otherWorker.send(data);
* }
* }
* }
*/
/////////
// Log //
/////////
var colors = require('colors');
var logHandle = fs.createWriteStream('./logs/cluster.log', {flags:"a"});
var log = function(msg, col){
var sqlDateTime = function(time){
if(time == null){ time = new Date(); }
var dateStr =
padDateDoubleStr(time.getFullYear()) +
"-" + padDateDoubleStr(1 + time.getMonth()) +
"-" + padDateDoubleStr(time.getDate()) +
" " + padDateDoubleStr(time.getHours()) +
":" + padDateDoubleStr(time.getMinutes()) +
":" + padDateDoubleStr(time.getSeconds());
return dateStr;
};
var padDateDoubleStr = function(i){
return (i < 10) ? "0" + i : "" + i;
};
msg = sqlDateTime() + " | " + msg;
logHandle.write(msg + "\r\n");
if(typeof col == "string"){col = [col];}
for(var i in col){
msg = colors[col[i]](msg);
}
console.log(msg);
}
//////////
// Main //
//////////
log(" - STARTING CLUSTER -", ["bold", "green"]);
cluster.on('exit', function(worker, code, signal) {
log("worker " + worker.process.pid + " (#"+worker.id+") has exited");
setTimeout(reloadWorker, 1000) // to prevent CPU-splsions if crashing too fast
});
function reloadWorker() {
var count = 0;
for( var i in cluster.workers ) count++;
if( count < numWorkers ) {
cluster.fork();
}
}
var restarting = false;
function rollingRestart() {
if( restarting ) return log('Cannot process new restart command while restarting.');
restarting = true;
var closingWorkers = [],
restartedWorkers = 0;
for( var i in cluster.workers ) {
closingWorkers.push(cluster.workers[i]);
}
restartOneWorker();
function restartOneWorker() {
if( restartedWorkers >= numWorkers ) return (restarting = false);
var closingWorker = closingWorkers.pop(),
worker = cluster.fork();
worker.on('listening', function() {
closingWorker.disconnect();
closingWorker.send('stop');
// Force close the worker after ten seconds.
setTimeout(function() {
closingWorker.destroy();
}, 10000);
restartedWorkers++;
setTimeout(restartOneWorker, 1000);
});
}
}