forked from captrespect/hallway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhallwayd.js
More file actions
319 lines (259 loc) · 7.97 KB
/
Copy pathhallwayd.js
File metadata and controls
319 lines (259 loc) · 7.97 KB
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
/*
*
* Copyright (C) 2011, Singly, Inc.
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/
exports.alive = false;
var async = require('async');
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.vital('hallwayd process id:', process.pid);
// temp measure to help apihosts have bigger pool
if (argv._.length === 0 || argv._[0] === "apihost") {
if(lconfig.database && lconfig.database.maxConnections) lconfig.database.maxConnections *= 2;
}
var taskmanNG = require('taskman-ng');
// avoid DEPTH_ZERO_SELF_SIGNED_CERT
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var http = require('http');
var https = require('https');
var dnsCache = require("dns-cache");
var dns = require("dns");
dns.lookup = dnsCache.cachedLookup;
// Reap the dns cache every 5m to refresh and prevent ram waste
setInterval(function() {
logger.info("Reaping dns cache.");
dnsCache.clearCache();
}, lconfig.dnsReapTime);
// Set our globalAgent sockets higher
http.globalAgent.maxSockets = 2048;
https.globalAgent.maxSockets = 2048;
function startAPIHost(cbDone) {
logger.vital("Starting an API hostess");
var webservice = require('webservice');
webservice.startService(lconfig.lockerPort, lconfig.lockerListenIP,
function () {
logger.vital('Hallway is now listening at ' + lconfig.lockerListenIP +
':' + lconfig.lockerPort);
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.");
process.exit(1);
}
logger.vital("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.vital("The Dawg is now monitoring at port %d", lconfig.dawg.port);
cbDone();
});
}
function startNexus(cbDone) {
logger.vital('Starting a Nexus. Should last about 4 years.');
require('nexusService').startService(
lconfig.nexus.port,
lconfig.nexus.listenIP,
cbDone
);
}
function startPod(cbDone) {
logger.vital('Starting a Pod so HAL can\'t hear us.');
require('podService').startService(
lconfig.pods.listenPort,
lconfig.pods.listenIP,
cbDone
);
}
function startStream(cbDone) {
logger.vital("Starting a Hallway Stream -- you're in for a good time.");
require('streamer').startService(lconfig.stream, function () {
logger.vital("Streaming at port %d", lconfig.stream.port);
cbDone();
});
}
function startWorkerSup(cbDone) {
var redis = require("redis");
var rclient = redis.createClient(lconfig.worker.redis.port || 6379,
lconfig.worker.redis.host || "127.0.0.1");
var pcron = require('pcron');
var pcronInst = pcron.init(rclient);
// Dynamically update lconfig.worker to include moduleName and args for
// invoking node
lconfig.worker.workerId = process.env.WORKER || require("os").hostname();
lconfig.worker.moduleName = "hallwayd.js";
lconfig.worker.spawnArgs = ["workerchild"];
// Monitor all services if unspecified
if (!lconfig.worker.services) {
var servezas = require('servezas');
servezas.load();
lconfig.worker.services = servezas.serviceList();
}
// Use pcronInsta.set_master to ensure we're not running gc_work/notify too
// often/heavily. We use a 10 second interval for simplicity; this means that
// each worker will run this script once every 10 seconds and then, if it's
// master, kick off gc_work/notify. Note that the "master" key is set to
// expire in 12 seconds to accomodate any lag that might happen.
var loop = function () {
pcronInst.set_master(lconfig.worker.workerId, 12000, function (err, result) {
if (err) {
logger.error("set_master failed: " + err);
} else if (result === 1) {
logger.debug("Won master lock; kicking pcronInst.notify/gc_work");
pcronInst.notify(lconfig.worker.services, Date.now(), function () {});
pcronInst.gc_work(lconfig.worker.services, Date.now(),
lconfig.worker.error_delay, function () {});
}
});
setTimeout(loop, 10000);
};
loop();
pcronInst.start_sup(lconfig.worker, function (err) {
if (err) {
logger.error("Failed to init pcron_sup: " + err);
process.exit(1);
}
cbDone();
});
}
function startWorkerChild(cbDone) {
taskmanNG.init(function () {
startWorkerWS(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.");
process.exit(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.vital("Starting a Hallway Worker, thou shalt be digitized",
lconfig.worker);
cbDone();
});
}
var Roles = {
workersup: {
startup: startWorkerSup
},
workerchild: {
startup: startWorkerChild
},
apihost: {
startup: startAPIHost
},
dawg: {
startup: startDawg
},
nexus: {
startup: startNexus
},
pod: {
startup: startPod
},
stream: {
startup: startStream
}
};
var rolename = 'apihost';
var role = Roles[rolename];
if (argv._.length > 0) {
rolename = argv._[0];
if (!Roles.hasOwnProperty(rolename)) {
logger.error("The %s role is unknown.", rolename);
process.exit(1);
}
role = Roles[rolename];
}
var startupTasks = [];
var podClient = require("podClient");
podClient.setRole(rolename);
if (role !== Roles.stream) {
// this loads all lib/services/*/map.js
startupTasks.push(function (cb) {
require('dMap').load();
require('servezas').load();
cb();
});
startupTasks.push(require('ijod').initDB);
startupTasks.push(require('tokenz').init);
startupTasks.push(require('taskList').init);
startupTasks.push(require('nexusClient').init);
var profileManager = require('profileManager');
startupTasks.push(profileManager.init);
profileManager.setRole(rolename);
}
if (role !== Roles.dawg && role !== Roles.stream) {
var acl = require('acl');
startupTasks.push(acl.init);
acl.setRole(rolename);
}
if (role.startup) {
startupTasks.push(role.startup);
}
async.series(startupTasks, function (err) {
if (err) {
logger.error('Error during startup', err);
process.exit(1);
}
logger.vital("Hallway is up and running.");
exports.alive = true;
});
process.on("SIGINT", function () {
logger.vital("Shutting down via SIGINT...");
switch (role) {
case Roles.worker:
taskmanNG.stop(function () {
process.exit(0);
});
break;
case Roles.apihost:
process.exit(0);
break;
default:
process.exit(0);
break;
}
});
process.on("SIGTERM", function () {
logger.vital("Shutting down via SIGTERM...");
process.exit(0);
});
process.on('uncaughtException', function (err) {
logger.warn('Uncaught exception:', err.stack);
instruments.increment('exceptions.uncaught').send();
// We try to ignore "innocous" errors. This is a temporary fix until we can
// track down source. TODO: Track down any/all root causes so we can get rid
// of this hack
// Check for errors we are comfortable (!!) ignoring
var ignoredErrors = [
// see: https://github.com/joyent/node/issues/2997
"Error: Parse Error",
"ECONNRESET",
"socket hangup",
"ETIMEDOUT",
"EADDRINFO"
];
var errString = err.toString();
for (var msg in ignoredErrors) {
if (errString.indexOf(ignoredErrors[msg]) >= 0) {
logger.warn("Ignored exception: ", ignoredErrors[msg]);
instruments.increment('exceptions.ignored').send();
return;
}
}
// None of the errors we know about -- shutdown
process.exit(1);
});