-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
228 lines (172 loc) · 7.76 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
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
'use strict';
/*
Copyright (c) 2013
Dave Williamson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var ConfigTool = require('./lib/configTool'),
pjson = require('./package.json'),
GracefulExit = require('./lib/gracefulexit'),
SendMessage = require('./lib/sendmessage'),
Startup = require('./lib/startup'),
Shutdown = require('./lib/shutdown'),
GarbageCollection = require('gc-stats'),
util = require('util'),
EventedStartup = require('./lib/eventedStartup');
require('colors');
var reportError = function reportError(message, err, data) {
var immediately = global.setImmediate || process.nextTick;
immediately(function () {
console.error('===============================| '.red + 'F I R E S T A R T E R E R R O R H A N D L E R'.yellow + ' |==============================='.red);
try {
console.error(message.red, err.stack.red);
if (data) {
console.error(util.inspect(data, {
colors: true,
showHidden: true
}));
}
} catch (ex) {
console.error('Error logging ERROR:', ex.stack);
}
console.error('=================================================================================================================='.red);
});
};
module.exports = function Firestarter(userConfig) {
var _self;
if (!(this instanceof Firestarter)) {
return new Firestarter(userConfig);
}
_self = this;
_self.config = new ConfigTool(userConfig);
_self.config.logger.info('Igniting the Firestarter (v' + pjson.version + ')!'.inverse.underline.yellow);
if (userConfig && userConfig.extendFirestarter) { userConfig.extendFirestarter(_self.config); }
_self.config.gracefulExit = new GracefulExit(_self.config);
_self.config.sendMessage = new SendMessage(_self.config);
_self.config.startup = new Startup(_self.config);
_self.config.shutdown = new Shutdown(_self.config);
_self.config.eventedStartup = new EventedStartup(_self.config)();
_self.config.HandlerRegistration = function HandlerRegistration(config) {
var _this;
if (!(this instanceof HandlerRegistration)) {
return new HandlerRegistration(config);
}
_this = this;
_this.config = config;
if (_this.config.memoryMonitoring) {
_this.gc = GarbageCollection();
_this.gc.on('stats', function gsStats(stats) {
// Exit if values missing
if (!stats || !stats.after) return;
var totalMem = stats.after.heapSizeLimit;
var usedMem = stats.after.totalHeapSize;
var percentageOfMemoryFree = (1 - (usedMem / totalMem));
// Exit if values too low
if (totalMem < 10 || usedMem < 10) return;
// _this.config.logger.info('Data from GC stats', '', percentageOfMemoryFree);
if (percentageOfMemoryFree < _this.config.restartProcessWhenMemoryPercentage) {
var err = new Error('Memory Leak')
_this.config.logger.warn('Restarting due to memory leak', '', stats);
reportError('Memory Leak: ', err);
_this.config.shutdown(err, 'Shutdown due to Memory Leak');
}
});
_this.config.restartProcessWhenMemoryPercentage = _this.config.restartProcessWhenMemoryPercentage / 100;
_self.startingMemory = process.memoryUsage();
_this.config.logger.warn('Memory Monitoring & Restarting enabled', '', _self.startingMemory);
}
process.once('uncaughtException', function (err) {
if (!(err instanceof Error)) {
err = new Error('Exeption did not get an error object: ' + err || 'undefined error');
}
reportError('Uncaught Exception: ', err);
_this.config.shutdown(err, 'Shutdown due to uncaughtException');
});
process.on('unhandledRejection', function (reason) {
if (!(reason instanceof Error)) {
reason = new Error('Rejection did not get an error object: ' + reason || 'undefined error');
}
reportError('Uncaught Exception: ', reason);
_this.config.shutdown(reason, 'Shutdown due to unhandledRejection');
});
process.once('SIGINT', function (err) {
if (!(err instanceof Error)) {
err = new Error('SIGINT shutdown (ctrl+c)');
}
reportError('Received shutdown message', err);
_this.config.shutdown(err, 'Shutdown due to SIGINT');
});
process.once('SIGTERM', function (err) {
if (!(err instanceof Error)) {
err = new Error('SIGTERM shutdown');
}
reportError('Received shutdown message', err);
_this.config.shutdown(err, 'Shutdown due to SIGTERM');
});
process.once('SIGHUP', function (err) {
if (!(err instanceof Error)) {
err = new Error('SIGHUP did not get an error object: ' + err || 'undefined error');
}
reportError('Received shutdown message from SIGHUP', err);
_this.config.shutdown(err, 'Shutdown due to SIGHUP');
});
process.on('message', function (message) {
var err;
if (message === 'ping') {
_this.config.sendMessage('pong');
} else if (message === 'shutdown') {
err = new Error('External shutdown requested: ' + message);
reportError('Received shutdown message from process instantiator', err);
_this.config.shutdown(err, 'Shutdown due to shutdown message');
} else {
_this.config.logger.warn('Received UNHANDLED message from process instantiator (forwarding to client):', '', {
message: message
});
}
});
return _this;
};
return {
config: _self.config,
shutdown: _self.config.shutdown,
startup: _self.config.startup,
eventedStartup: function () {
return _self.config.eventedStartup;
},
getApp: function () {
return _self.config.app;
},
getServer: function () {
return _self.config.server;
},
getSpdyServer: function () {
return _self.config.spdyServer;
},
getHttp: function () {
return _self.config.http;
},
getServerDomain: function () {
return _self.config.serverDomain;
},
getSocketIO: function () {
return _self.config.sioObj;
},
getSecureSocketIO: function () {
return _self.config.secureSioObj;
}
};
};