forked from captrespect/hallway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hallwayd.js
279 lines (219 loc) · 6.55 KB
/
hallwayd.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
/*
*
* Copyright (C) 2011, The Locker Project
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/
exports.alive = false;
var async = require('async');
var util = require('util');
var argv = require('optimist').argv;
// lconfig has to be loaded before any other hallway modules!
var lconfig = require('lconfig');
var instruments = require('instruments');
var logger = require('logger').logger('hallwayd');
logger.info('process id:' + process.pid);
var alerting = require("alerting");
function exit(returnCode) {
logger.info("Shutdown complete");
process.exit(returnCode);
}
var shuttingDown_ = false;
// scheduling and misc things
function shutdown(returnCode, callback) {
if (shuttingDown_ && returnCode !== 0) {
try {
console.error("Aieee! Shutdown called while already shutting down! " +
"Panicking!");
}
catch (e) {
// we tried...
}
process.exit(1);
}
shuttingDown_ = true;
process.stdout.write("\n");
logger.info("Shutting down...");
if (callback) {
return callback(returnCode);
}
exit(returnCode);
}
if (lconfig.alerting && lconfig.alerting.key) {
alerting.init(lconfig.alerting);
alerting.install(function (E) {
logger.error("Uncaught exception: %s", E.message);
shutdown(1);
});
}
var taskman = require('taskman');
var profileManager = require('profileManager');
var http = require('http');
// Set our globalAgent sockets higher
http.globalAgent.maxSockets = 800;
function startAPIHost(cbDone) {
logger.info("Starting an API host");
var webservice = require('webservice');
webservice.startService(lconfig.lockerPort, lconfig.lockerListenIP,
function (hallway) {
logger.info('Hallway is now listening at ' + lconfig.lockerBase);
cbDone();
});
}
function startDawg(cbDone) {
if (!lconfig.dawg || !lconfig.dawg.port || !lconfig.dawg.password) {
logger.error("You must specify a dawg section with at least a port and " +
"password to run.");
shutdown(1);
}
logger.info("Starting a Hallway Dawg -- Think you can get away without " +
"having a hall pass? Think again.");
var dawg = require('dawg');
dawg.startService(lconfig.dawg.port, lconfig.dawg.listenIP, function() {
logger.info("The Dawg is now monitoring at port %d", lconfig.dawg.port);
cbDone();
});
}
function startStream(cbDone) {
logger.info("Starting a Hallway Stream -- you're in for a good time.");
require('streamer').startService(lconfig.stream, function() {
logger.info("Streaming at port %d", lconfig.stream.port);
cbDone();
});
}
function startWorkerWS(cbDone) {
if (!lconfig.worker || !lconfig.worker.port) {
logger.error("You must specify a worker section with at least a port and " +
"password to run.");
shutdown(1);
}
var worker = require("worker");
if (!lconfig.worker.listenIP) lconfig.worker.listenIP = "0.0.0.0";
worker.startService(lconfig.worker.port, lconfig.worker.listenIP, function() {
logger.info("Starting a Hallway Worker, thou shalt be digitized",
lconfig.worker);
cbDone();
});
}
var Roles = {
worker: {
startup: startWorkerWS
},
apihost: {
startup: startAPIHost
},
dawg: {
startup: startDawg
},
stream: {
startup: startStream
}
};
var role = Roles.apihost;
function startTaskman(cbDone) {
var isWorker = (role === Roles.worker);
if (isWorker) logger.info("Starting a worker.");
taskman.init(argv.pid, isWorker, argv.once, cbDone);
}
if (argv._.length > 0) {
if (!Roles.hasOwnProperty(argv._[0])) {
logger.error("The %s role is unknown.", argv._[0]);
return shutdown(1);
}
role = Roles[argv._[0]];
}
var startupTasks = [];
if (role !== Roles.stream) {
// this loads all lib/services/*/map.js
startupTasks.push(require('dMap').startup);
startupTasks.push(require('ijod').initDB);
startupTasks.push(startTaskman);
}
if (role !== Roles.dawg && role !== Roles.stream) {
startupTasks.push(require('acl').init);
startupTasks.push(profileManager.init);
}
if (role.startup) {
startupTasks.push(role.startup);
}
async.series(startupTasks, function(error) {
// TODO: This needs a cleanup, it's too async
logger.info("Hallway is up and running.");
exports.alive = true;
});
process.on("SIGINT", function() {
logger.info("Shutting down via SIGINT...");
switch (role) {
case Roles.worker:
taskman.stop(function() {
shutdown(0);
});
break;
case Roles.apihost:
shutdown(0);
break;
default:
shutdown(0);
break;
}
});
process.on("SIGTERM", function () {
logger.info("Shutting down via SIGTERM...");
shutdown(0);
});
if (!process.env.LOCKER_TEST) {
process.on('uncaughtException', function (err) {
try {
// copy of these in alerting.js so they don't fire alerts too
var E = err;
if (E.toString().indexOf('Error: Parse Error') >= 0) {
// ignoring this for now, relating to some node bug,
// https://github.com/joyent/node/issues/2997
logger.warn("ignored exception", E);
instruments.increment('exceptions.ignored').send();
return;
}
if (E.toString().indexOf('ECONNRESET') >= 0 ||
E.toString().indexOf('socket hang up') >= 0) {
// THEORY: these bubble up from event emitter as uncaught errors, even
// though the socket end event still fires and are ignorable
logger.warn("ignored exception", E);
instruments.increment('exceptions.ignored').send();
return;
}
if (E.toString().indexOf('ETIMEDOUT') >= 0) {
// THEORY: these bubble up from event emitter as uncaught errors, even
// though the socket end event still fires and are ignorable
logger.warn("ignored exception", E);
instruments.increment('exceptions.ignored').send();
return;
}
if (E.toString().indexOf('EADDRINFO') >= 0) {
logger.warn("ignored exception", E);
instruments.increment('exceptions.ignored').send();
return;
}
logger.error('Uncaught exception:');
logger.error(util.inspect(err));
instruments.increment('exceptions.uncaught').send();
if (err && err.stack) {
logger.error(util.inspect(err.stack));
}
shutdown(1);
} catch (e) {
try {
console.error("Caught an exception while handling an uncaught " +
"exception!");
console.error(e);
} catch (e) {
// we tried...
}
process.exit(1);
}
});
}
// Export some things so this can be used by other processes,
// mainly for the test runner
exports.shutdown = shutdown;