-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·277 lines (245 loc) · 8.48 KB
/
server.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
#!/usr/bin/env node
var VERSION = "0.1.0";
var catchExceptions = true;
var args = { /* defaults */
port: "9000",
apiport: "7000",
apihost: "127.0.0.1",
debug: false,
loglevel: 'debug',
logfile: false,
logtimestamps: false,
"node-env": 'development'
};
/* Parse command line options */
var pattern = /^--(.*?)(?:=(.*))?$/;
process.argv.forEach(function(value) {
var match = pattern.exec(value);
if (match) {
args[match[1]] = match[2] ? match[2] : true;
}
});
var logLevels = {
"debug3": 0,
"debug2": 2,
"debug": 4,
"info": 6,
"warn": 8,
"error": 10,
"fatal": 12
};
var port = parseInt(args['port']);
var listenip = args['listenip'];
var apiport = parseInt(args['apiport']);
var apilistenip = args['apihost'];
var serverId = args['serverid'];
var showHelp = args['help'];
var logLevel = logLevels[args['loglevel'].toLowerCase()];
var logfile = args['logfile'];
var logtimestamps = args['logtimestamps'];
var nodeEnv = args['node-env'];
process.env['NODE_ENV'] = nodeEnv;
console.log("Worlize Chat Server Version " + VERSION);
console.log("--------------------------------------------");
if (showHelp) {
console.log("usage: server.js [Options]");
console.log("");
console.log("options:");
console.log(" --help Show this help information");
console.log("");
console.log(" --port=nnnn Listen on port nnnn (Optional) Defaults to 9000");
console.log("");
console.log(" --serverid=mysrv1 Set server identifier to mysrv1 (Optional)");
console.log(" Defaults to <hostname>-<port>, e.g. server.foo.com-9000");
console.log("");
console.log(" --listenip=<ip> Listen on the specified IP address only. Default is to");
console.log(" listen on all interfaces.");
console.log("");
console.log(" --loglevel=<level> Set log level. Default is debug.");
console.log(" Accepted values, in order of increasing verbosity:");
console.log(" fatal, error, warn, info, debug, debug2, debug3");
console.log("");
console.log(" --logfile=<filename> Log output to a file instead of stdout.");
console.log("");
console.log(" --logtimestamps Include timestamps in log output.");
console.log("");
console.log(" --apiport=<port> Specify the port that the internal REST API service");
console.log(" listen on.");
console.log("");
console.log(" --apihost=<ip> Specify which IP address the internal REST API should");
console.log(" listen on.");
console.log("");
console.log(" --node-env=<env> Use 'production' to run in production mode, or");
console.log(" use 'development' to run in development mode.");
console.log("");
process.exit(0);
}
else {
console.log("For usage information, run with --help.");
}
if (typeof(logLevel) !== 'number') {
console.log("Invalid log level: " + args['loglevel']);
process.exit(1);
}
else {
console.log("Log level: " + args['loglevel']);
}
var spawn = require('child_process').spawn,
os = require('os'),
http = require('http'),
ChatServer = require('./lib/chat_server'),
PresenceServer = require('./lib/presence_server'),
InternalAPIServer = require('./lib/api/internal'),
WebSocketServer = require('websocket').server,
WebSocketRouter = require('websocket').router,
redisConnectionManager = require('./lib/model/redis_connection_manager'),
Log = require('./lib/util/log'),
ConsoleTarget = require('./lib/util/console_target'),
FileTarget = require('./lib/util/file_target'),
heapdump = require('heapdump');
var logger = Log.getLogger('main');
var logTarget;
var shutdownLogTarget;
if (typeof(logfile) === 'string') {
logTarget = new FileTarget();
logTarget.logFilePath = logfile;
console.log("Logging to " + logfile);
}
else {
logTarget = new ConsoleTarget();
}
logTarget.logTime = logtimestamps;
logTarget.logIdentifier = true;
logTarget.logSeverity = true;
logTarget.logLevel = logLevel;
logTarget.instanceId = serverId;
logTarget.logInstanceId = true;
Log.addTarget(logTarget);
logger.info('Starting Worlize Interactivity Server ' + VERSION);
var terminationRequests = 0;
var server;
var httpServer;
var webSocketServer;
var webSocketRouter;
var chatServer;
var presenceServer;
function listen(port, listenip, serverId) {
logger.info("Server ID: " + serverId);
httpServer = http.createServer(function(req, res) {
logger.info("Request for url " + req.url);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("Nothing to see here. Move along.\n");
});
if (listenip) {
httpServer.listen(port, listenip, function() {
logger.info("Listening on " + listenip + ":" + port);
});
}
else {
httpServer.listen(port, function() {
logger.info("Listening on 0.0.0.0:" + port);
});
}
webSocketServer = new WebSocketServer({
// config options...
httpServer: httpServer
});
webSocketRouter = new WebSocketRouter();
webSocketRouter.attachServer(webSocketServer);
chatServer = new ChatServer(serverId);
chatServer.debug = args.debug ? true : false;
chatServer.mount( webSocketRouter );
presenceServer = new PresenceServer();
presenceServer.debug = args.debug ? true : false;
presenceServer.mount( webSocketRouter, serverId );
InternalAPIServer.start({
host: apilistenip,
port: apiport,
serverId: serverId
});
apiServer = InternalAPIServer.app;
}
if (typeof(port) == 'number' && port !== NaN && port > 0 && port < 65535) {
if (serverId) {
listen(port, listenip, serverId);
}
else {
var hostname = os.hostname();
serverId = hostname + "-" + port;
listen(port, listenip, serverId);
}
}
else {
logger.fatal("You must specify a valid port number");
if (logTarget.close) {
logTarget.close();
}
process.exit(1);
}
function handleSignalToTerminate() {
if (!shutdownLogTarget && typeof(logfile) === 'string') {
shutdownLogTarget = new ConsoleTarget();
shutdownLogTarget.logTime = false;
shutdownLogTarget.logIdentifier = true;
shutdownLogTarget.logNotation = false;
shutdownLogTarget.logSeverity = false;
shutdownLogTarget.logLevel = logLevels['info'];
shutdownLogTarget.logInstanceId = false;
Log.addTarget(shutdownLogTarget);
}
terminationRequests ++;
if (terminationRequests === 2) {
logger.info("Received two termination requests. Exiting Immediately.");
if (logTarget.close) {
logTarget.close();
}
process.exit(1);
}
logger.info("Shutting down...");
var shutdownFailedTimeout;
var callbacksRemaining = 2;
function handleShutdownComplete() {
callbacksRemaining --;
if (callbacksRemaining === 0) {
redisConnectionManager.shutDown();
webSocketServer.shutDown();
setTimeout(function() {
logger.info("Shutdown complete");
if (shutdownFailedTimeout) {
clearTimeout(shutdownFailedTimeout);
}
if (logTarget.close) {
logTarget.close();
}
process.exit(0);
}, 1000);
}
}
shutdownFailedTimeout = setTimeout(function() {
logger.fatal("Graceful shutdown failed. Terminating.");
redisConnectionManager.shutDown();
webSocketServer.shutDown();
setTimeout(function() {
if (logTarget.close) {
logTarget.close();
}
process.exit(1);
}, 1000);
}, 8000);
// Stop accepting new connections.
httpServer.close();
chatServer.shutDown(handleShutdownComplete);
presenceServer.shutDown(handleShutdownComplete);
}
process.on('SIGINT', handleSignalToTerminate);
process.on('SIGTERM', handleSignalToTerminate);
if (catchExceptions) {
// Top level exception handler, for safety. Log stack trace.
process.addListener('uncaughtException', function(error) {
var logObject = {
stackTrace: error.stack
};
logger.fatal("Uncaught Exception: " + JSON.stringify(logObject));
logger.debug("Stack Trace:\n" + error.stack);
});
}