forked from geddy/geddy
/
master.js
430 lines (385 loc) · 12.7 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
var Master
, cluster
, fs = require('fs')
, path = require('path')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, errors = require('../response/errors')
, watchFiles = require('../watch_files')
, Log = require('../../deps/log')
, dispatch = require('./master_dispatch')
, fileUtils = require('../utils/file')
, dateUtils = require('../utils/date').date;
if (!geddy.FD_HACK) {
cluster = require('cluster');
}
var processModes = {
KEEP_ALIVE: 'keepAlive'
, SHUTDOWN: 'shutdown'
, WATCH_FILES: 'watchFiles'
};
Master = function () {
var self = this;
this.init();
// Clustering-only
cluster.addListener('death', function (worker) {
var id = worker.pid.toString();
self.handleWorkerExit(id);
});
};
Master.prototype = new (function () {
var _readConfig = function (next) {
var opts = this.opts
, dir = process.cwd()
, dirList = fs.readdirSync(dir + '/config')
, fileName
, fileExt
, fileBaseName
, usingCoffee
, appBaseConfig
, appEnvConfig;
this.config = require('../base_config');
this.config.environment = opts.environment || this.config.environment;
// Base config for workers-count should be 1 in dev-mode
// Cycle based on filesystem changes, not keep-alive
// Process-rotation not possible in this mode
if (this.config.environment == 'development') {
this.config.workers = 1;
this.processMode = processModes.WATCH_FILES;
this.config.rotateWorkers = false;
}
// App configs
for (var i = 0; i < dirList.length; i++) {
fileName = dirList[i];
fileExt = path.extname(fileName);
fileBaseName = path.basename(fileName, fileExt);
// Require the environment configuration and the base configuration file
if(fileBaseName === this.config.environment || fileBaseName === 'environment') {
if(fileExt === '.coffee') {
// fileName is a CoffeeScript file so try to require it
try {
usingCoffee = usingCoffee || require('coffee-script');
} catch(err) {
new errors.coffeeError();
}
}
appBaseConfig = require(dir + '/config/environment');
appEnvConfig = require(dir + '/config/' + this.config.environment);
}
}
// Start with a blank slate, mix everything in
geddy.mixin(this.config, appBaseConfig);
geddy.mixin(this.config, appEnvConfig);
geddy.mixin(this.config, opts);
// Obvious, don't rotate with only one worker
if (this.config.workers < 2) {
this.config.rotateWorkers = false;
}
next();
}
, _initLogging = function (next) {
var self = this
, levelsByType
, stdoutLevel
, types = ['stdout', 'stderr', 'access']
, now = dateUtils.strftime(new Date(), '%FT%T')
, loggly
, dir = this.config.logDir
, rotateAndInitByType;
// Set the logging level for stdout
if (this.config.debug) {
stdoutLevel = Log.DEBUG;
}
else if (this.config.logLevel) {
stdoutLevel = Log[this.config.logLevel]();
}
else {
stdoutLevel = Log.INFO; // Default to info
}
// Now that we have the desired level for stdout, set up log levels by key
levelsByType = {
access: 'access'
, stderr: 'error'
, stdout: stdoutLevel
};
// Create the log directory if it doesn't exist
fileUtils.mkdirP(dir);
types.forEach(function (type) {
var currentLog = path.join(dir, type + '.log')
, archivedLog = path.join(dir, type + '.' + now + '.log');
if (existsSync(currentLog)) {
try {
fs.renameSync(currentLog, archivedLog);
}
catch(e) {
// cpR doesn't work correctly on Windows either, just blow
// away the fucking log
//fileUtils.cpR(currentLog, archivedLog);
fs.unlinkSync(currentLog);
}
}
// After the file is renmaed, create the new logger with the original filename
// e.g., access.log
self[type + 'Log'] = new Log(levelsByType[type],
fs.createWriteStream(dir +
'/' + type + '.log'), true, loggly);
self[type + 'Log'].type = type;
});
this.stdoutLog.info('Server starting with config: ' +
JSON.stringify(self.config, true, 2));
next();
}
, _startMetrics = function (next) {
var metrics
, port;
if (this.config.metrics) {
try {
port = this.config.metrics.port;
metrics = require(process.cwd() + '/node_modules/metrics');
this.stdoutLog.info('Metrics server started on port ' + port);
this.metricsServer = new metrics.Server(port);
}
catch(e) {
throw new Error('To turn on Metrics, do `npm install metrics`. ' +
'Could not find it at: ' + process.cwd() + '/node_modules/metrics');
}
}
next();
}
, _monitorProcesses = function (next) {
// Stop spawning new worker-processes when shutting down
if (this.processMode == processModes.SHUTDOWN) {
return;
}
var self = this
, workers = this.workers
, count = this.config.workers
, now = (new Date()).getTime()
, heartbeatWindow = this.config.heartbeatWindow;
workers.each(function (data, id) {
var worker = data.worker
, killWorker = false;
// Process-rotation fu
if (self.config.rotateWorkers) {
// If the process takes too long to retire, just kill it
if (data.retired) {
if (!data.killed) {
self.stderrLog.warning('Process ' +
worker.pid + ' took too long to retire, killing process.');
if (now > (data.retireAt + self.config.rotationTimeout + 5000)){
killWorker = true;
}
}
}
else {
// If this worker is past its freshness date, retire it
if (now > data.retireAt) {
self.stdoutLog.info('Rotating ' +
worker.pid + ', killing process.');
data.retired = true;
self.sendMessage(id, {
method: 'retire'
});
}
}
}
// Kill if process hasn't called back in a while
if ((now - worker.heartbeatAt) > heartbeatWindow) {
if (!data.killed) {
killWorker = true;
self.stderrLog.warning('No current heartbeat from ' +
worker.pid + ', killing process.');
}
}
// Kill, manual-restart from code-changes in dev-mode
if (self.processMode == processModes.WATCH_FILES) {
if (!data.killed) {
killWorker = true;
self.stderrLog.warning('Code changed, killing ' +
worker.pid + ', cycling ...');
}
}
if (killWorker) {
self.killWorker(worker.pid);
}
});
this.createWorkers();;
// If called manually, not part of the normal polling loop
if (this.processMode == processModes.KEEP_ALIVE) {
setTimeout(function () {
_monitorProcesses.call(self);
}, 5000);
}
// Initial async-chain invocation during startup
if (typeof next === 'function') {
next();
}
}
, _watchFiles = function () {
var self = this
, dir = process.cwd()
, callback = function (curr, prev) {
if (curr.mtime.getTime() != prev.mtime.getTime()) {
_monitorProcesses.call(self, {manualRotate: true});
}
};
// Watch individual files so we can compare mtimes and restart
// on code-changes
watchFiles.watch(dir + '/config', callback);
watchFiles.watch(dir + '/lib', callback);
watchFiles.watch(dir + '/app/controllers', callback);
watchFiles.watch(dir + '/app/models', callback);
watchFiles.watch(dir + '/app/views', callback);
};
this.init = function () {
var self = this;
this.opts = null;
this.config = {};
this.workers = new geddy.SortedCollection();
this.processMode = processModes.KEEP_ALIVE;
// Don't bother with graceful shutdown for win32
if (!process.platform == 'win32') {
process.addListener('SIGTERM', function () {
self.startShutdown();
});
}
};
this.start = function (options) {
var self = this
, items
, chain;
this.opts = options || {};
items = [
_readConfig
, _initLogging
, _startMetrics
, _monitorProcesses
];
chain = new geddy.async.SimpleAsyncChain(items, this);
chain.last = function () {
// Watchfiles is apparently still broken in win32
if (self.config.environment == 'development' &&
process.platform != 'win32') {
_watchFiles.call(self);
}
};
chain.run();
};
this.createWorkers = function () {
var configCount = this.config.workers
, currentCount = this.workers.count
, needed = configCount - currentCount
, rotationWindow = this.config.rotationWindow
, staggerInterval = rotationWindow / needed
, retirement = (new Date()).getTime() + rotationWindow
, msg;
if (needed) {
msg = 'Creating ' + needed + ' worker process';
msg += needed > 1 ? 'es.' : '.';
this.stdoutLog.info(msg);
while (currentCount < configCount) {
currentCount++;
this.createWorker(retirement);
retirement -= staggerInterval;
}
}
};
this.createWorker = function (dt) {
var self = this
, retireAt = dt || (new Date()).getTime() + this.config.rotationWindow
, w = cluster.fork()
, id = w.pid.toString()
, data = new WorkerData(id, w);
this.workers.addItem(id, data, retireAt);
this.addWorkerListeners(w);
this.sendMessage(id, {
method: 'config'
, data: this.config
});
};
this.addWorkerListeners = function (worker) {
var self = this;
worker.addListener('message', function (msg) {
self.receiveMessage(msg);
});
};
this.sendMessage = function (workerId, msg) {
var id = workerId.toString()
, worker = this.workers.getItem(id).worker;
try {
worker.send(msg);
}
catch (err) {
this.killWorker(workerId);
}
};
this.receiveMessage = function (msg) {
if (msg && msg.method && dispatch[msg.method]) {
dispatch[msg.method].call(this, msg);
}
else {
if (!msg.cmd) {
console.log(JSON.stringify(msg));
}
}
};
this.killWorker = function (pid) {
var self = this
, id = pid.toString()
, worker = this.workers.getItem(id);
worker.killed = true;
try {
process.kill(pid);
}
catch(e) {}
// handleWorkerExit gets called on the process die/exit event.
// If for some reason it doesn't get called, we still want
// the process removed from the list of active proceses
setTimeout(function () {
self.handleWorkerExit(pid);
}, 20000);
};
this.handleWorkerExit = function (pid) {
var id = pid.toString()
, worker = this.workers.getItem(id);
if (worker && !worker.killed) {
this.stderrLog.error('Worker ' + id + ' died.');
}
this.workers.removeItem(id);
// As each worker exits, check to see if all processes
// have exited and we can kill the master process
if (this.processMode == processModes.SHUTDOWN) {
this.checkShutdown();
}
};
this.startShutdown = function () {
var self = this;
this.processMode = processModes.SHUTDOWN;
this.stdoutLog.notice('Graceful shutdown from SIGTERM...');
this.workers.each(function (data) {
self.sendMessage(data.id, {
method: 'shutdown'
});
});
};
this.checkShutdown = function () {
if (!this.workers.count) {
this.shutDown();
}
};
this.shutDown = function () {
process.exit();
};
})();
WorkerData = function (id, worker, retireAt) {
this.id = id;
this.heartbeatAt = (new Date()).getTime();
this.process = null;
this.pid = null;
this.worker = worker;
this.retireAt = retireAt;
this.retired = false;
this.killed = false;
};
module.exports.Master = Master;
module.exports.WorkerData = WorkerData;