forked from mdn/kumascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
83 lines (71 loc) · 2.17 KB
/
run.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
// ## Kumascript server runner
//
// Command line KumaScript server runner
/*jshint node: true, expr: false, boss: true */
// ### Prerequisites
var util = require('util'),
http = require('http'),
net = require('net'),
_ = require('underscore'),
winston = require('winston'),
kumascript = require(__dirname),
ks_conf = kumascript.conf,
ks_utils = kumascript.utils,
ks_server = kumascript.server,
ks_repl = kumascript.repl;
// ### Initialize logging
var log_conf = ks_conf.nconf.get('log');
if (!log_conf.console) {
winston.remove(winston.transports.Console);
}
if (log_conf.file) {
// TODO: Need a rotating file logger here!
winston.add(winston.transports.File, log_conf.file);
}
// TODO: Accept log line format from config?
// TODO: Use [winston-syslog](https://github.com/indexzero/winston-syslog)?
// Make a nicer alias to the default logger
var log = winston;
var statsd = ks_utils.getStatsD({
statsd_conf: ks_conf.nconf.get('statsd')
});
var server_conf = ks_conf.nconf.get('server');
server_conf.statsd = statsd;
// Start up a server instance.
var port = server_conf.port;
log.info("Worker PID " + process.pid + " starting on port " + port);
var server = new ks_server.Server(server_conf);
server.listen(port);
// Open up a telnet REPL for interactive access to the server.
var repl = null;
var repl_config = ks_conf.nconf.get('repl');
if (repl_config.enabled) {
repl = new ks_repl.REPL(repl_config, {
__: _,
log: log,
server_conf: server_conf,
kill: performExit,
server: server
});
repl.listen(repl_config.host, repl_config.port);
}
function performExit () {
log.info("Master PID " + process.pid + " exiting");
server.close();
if (repl) { repl.close(); }
process.exit(0);
}
// More gracefully handle some common exit conditions...
process.on('SIGINT', function () {
log.info("Received SIGINT, exiting...");
performExit();
});
process.on('SIGTERM', function () {
log.info("Received SIGTERM, exiting...");
performExit();
});
process.on('uncaughtException', function (err) {
log.error('uncaughtException:', err.message);
log.error(err.stack);
performExit();
});