-
Notifications
You must be signed in to change notification settings - Fork 34
/
hallwayd.js
263 lines (238 loc) · 7.45 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
/*
*
* Copyright (C) 2011, The Locker Project
* All rights reserved.
*
* Please see the LICENSE file for more information.
*
*/
exports.alive = false;
var fs = require('fs');
var path = require('path');
var async = require('async');
var util = require('util');
var argv = require("optimist").argv;
var Roles = {
worker: {
startup:startWorkerWS
},
apihost: {
startup:startAPIHost
},
dawg: {
startup:startDawg
},
stream: {
startup:startStream
}
};
var role = Roles.apihost;
// This lconfig stuff has to come before any other hallway modules are loaded!
var lconfig = require('lconfig');
var configDir = process.env.LOCKER_CONFIG || 'Config';
if (!lconfig.loaded) {
var configFile;
if (process.argv[2] === '--config') {
configFile = process.argv[3];
}
else {
configFile = path.join(configDir, 'config.json');
}
lconfig.load(configFile);
}
else {
console.warn("Hallway config already loaded");
}
var logger = require("logger").logger("hallwayd");
logger.info('process id:' + process.pid);
var alerting = require("alerting");
lconfig.alerting = {key:1}
if (lconfig.alerting && lconfig.alerting.key) {
alerting.init(lconfig.alerting);
alerting.install(function(E) {
logger.error("Uncaught exception: %s", E.message);
shutdown(1);
});
}
var syncManager = require("syncManager.js");
var pipeline = require('pipeline');
var profileManager = require('profileManager');
// Set our globalAgent sockets higher
var http = require("http");
http.globalAgent.maxSockets = 800;
if (process.argv.indexOf("offline") >= 0) syncManager.manager.offlineMode = true;
var shuttingDown_ = false;
function syncComplete(response, task, runInfo, callback) {
logger.info("Got a completion from %s", task.profile);
if(!response) logger.debug("missing response");
if(!response) response = {};
pipeline.inject(response.data, runInfo.auth, function(err) {
if(err) return logger.error("failed pipeline processing: "+err);
logger.verbose("Rescheduling " + JSON.stringify(task) + " and config "+JSON.stringify(response.config));
// save any changes and reschedule
var nextRun = response.config && response.config.nextRun;
if(nextRun) delete response.config.nextRun; // don't want this getting stored!
async.series([
function(cb) { if(!response.auth) return cb(); profileManager.authSet(task.profile, response.auth, null, cb) },
function(cb) { if(!response.config) return cb(); profileManager.configSet(task.profile, response.config, cb) },
function(cb) { syncManager.manager.schedule(task, nextRun, cb); },
], callback);
})
}
function startSyncmanager(cbDone) {
var isWorker = (role === Roles.worker);
if (isWorker) {
syncManager.manager.completed = syncComplete;
logger.info("Starting a worker.");
}
syncManager.manager.init(isWorker, function() {
cbDone();
});
}
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");
if (!lconfig.dawg.listenIP) lconfig.dawg.listenIP = "0.0.0.0";
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(syncManager.manager, lconfig.worker.port, lconfig.worker.listenIP, function() {
logger.info("Starting a Hallway Worker, thou shalt be digitized", lconfig.worker);
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) {
startupTasks.push(require('ijod').initDB);
}
if (role !== Roles.dawg && role !== Roles.stream) {
startupTasks.push(startSyncmanager);
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;
});
// 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);
}
else {
return exit(returnCode);
}
}
function exit(returnCode) {
logger.info("Shutdown complete");
process.exit(returnCode);
}
process.on("SIGINT", function() {
logger.info("Shutting down via SIGINT...");
switch (role) {
case Roles.worker:
syncManager.manager.stop(function() {
shutdown(0);
});
break;
case Roles.apihost:
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 {
if(err.toString().indexOf('Error: Parse Error') >= 0)
{
// ignoring this for now, relating to some node bug, https://github.com/joyent/node/issues/2997
logger.warn(err);
return;
}
if(err.toString().indexOf('ECONNRESET') >= 0 || err.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(err);
return;
}
logger.error('Uncaught exception:');
logger.error(util.inspect(err));
if (err && err.stack) logger.error(util.inspect(err.stack));
if (lconfig.airbrakeKey) {
var airbrake = require('airbrake').createClient(lconfig.airbrakeKey);
airbrake.notify(err, function(err, url) {
if (url) logger.error(url);
shutdown(1);
});
} else {
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;