-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (96 loc) · 2.65 KB
/
index.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
'use strict';
process.env.SUPPRESS_NO_CONFIG_WARNING = 'y';
var _ = require('lodash'),
Config = require('Config'),
cluster = require('cluster'),
Logger = require('./utils/Logger');
var BootDefaultConfigs = {
cooldown: 25,
retries: 5,
processes: 4
};
module.exports = function(configs) {
Config.util.extendDeep(BootDefaultConfigs, configs);
Config.util.setModuleDefaults('Boot', BootDefaultConfigs);
function Moraine(){
return;
}
Moraine.Injector = require('./utils/Injector');
Moraine.Injector.register('Logger', Logger);
Moraine.Injector.register('Config', Config);
var cooldown = Config.get('Boot.cooldown'),
processes = Config.get('Boot.processes'),
_processes = [],
restarts = [],
retries = Config.get('Boot.retries');
function restart() {
var now = new Date().getTime();
restarts = restarts.filter(function (restartTime) {
return restartTime > (now - cooldown * 60 * 1000);
});
restarts.push(now);
if (restarts.length < retries) {
cluster.fork();
return true;
}
return false;
}
function launch() {
var domain = require('domain');
var d = domain.create();
d.on('error', function (er) {
Logger.error('Process %s has failed', process.pid);
Logger.error('error', er.stack);
try {
// make sure we close down within 30 seconds
var killtimer = setTimeout(function () {
process.exit(1);
}, 30000);
// But don't keep the process open just for that!
killtimer.unref();
// trigger a 'disconnect' in the cluster master
if (cluster) {
cluster.worker.disconnect();
}
} catch (er2) {
// oh well, not much we can do at this point.
Logger.error('Error handling gracefully', er2.stack);
}
});
// Now run the handler function in the domain.
d.run(function () {
Logger.info('starting cluster with pid: ' + process.pid);
_.each(_processes, function(_process){
Moraine.Injector.process(_process);
});
});
}
Moraine.start = function start() {
if (cluster.isMaster) {
Logger.info('started master with pid: ' + process.pid);
for (var i = 0; i < processes; i++) {
cluster.fork();
}
cluster.on('disconnect', function (worker) {
if (!restart()) {
Logger.info('too many restarts within the last %s minutes...', cooldown);
var chillout = setTimeout(function () {
restart();
}, cooldown * 60 * 1000);
chillout.unref();
}
});
process.on('exit', function (code) {
Logger.info('Master process died with code: ' + code);
});
} else {
launch();
}
};
Moraine.register = function (func){
_processes.push(func);
}
return new Promise(function (resolve, reject) {
resolve(Moraine);
});
}