-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
125 lines (110 loc) · 3.24 KB
/
app.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
// app.js
/* eslint no-console: "off" */
const isNil = require('lodash.isnil');
const fs = require('fs');
const program = require('commander');
const pack = require('./package.json');
const Server = require('./app/server.js');
const Logger = require('./app/lib/logger');
let config = require('./config/config');
/**
* Class representing the app
* @class App
*/
class App {
constructor() {
this.logger = {};
this.log = {};
this.setupLogging();
this.commandHandler();
}
setupLogging() {
this.logger = new Logger(config);
this.log = this.logger.log;
}
//
commandHandler() {
program
.version(pack.version)
.option('-c, --config <filename>', 'Use the specified configuration file instead of the file in ./config/');
program.on('--help', () => {
console.log('');
console.log(` Uriel v${pack.version}`);
});
program.parse(process.argv);
if (program.config) {
this.log.info('Loading external configuration...');
let results;
try {
if (program.config.substr(-3) === '.js') {
config = require(program.config);
} else if (program.config.substr(-5) === '.json') {
results = fs.readFileSync(program.config);
config = JSON.parse(results);
} else {
this.log.info('Invalid file provided, external configuration must end with .js or .json');
this.log.info('Falling back to default config');
}
this.log.debug('Settings:');
this.log.debug(`Polling Timer: ${config.server.pollingTimer}`);
this.log.debug(`StatsD Host: ${config.statsd.host}:${config.statsd.port}`);
this.log.debug(`Server Name: ${config.statsd.name}`);
} catch (err) {
console.log(err.stack || err);
process.exit(1);
}
}
}
// ****************************************************************************
// Application Shutdown Logic
// ***************************************************************************/
handleSIGTERM() {
this.close(15);
}
handleSIGINT() {
this.close(2);
}
close(code) {
let sigCode;
code = code || 0;
switch (code) {
case 2:
sigCode = 'SIGINT';
break;
case 15:
sigCode = 'SIGTERM';
break;
default:
sigCode = code;
break;
}
// Perform gracful shutdown here
this.log.info(`Received exit code ${sigCode}, performing graceful shutdown`);
if (!isNil(this.server)) {
this.server.close();
}
// Shutdown the server
// End the process after allowing time to close cleanly
setTimeout(
(errCode) => {
process.exit(errCode);
},
config.server.shutdownTime,
code
);
}
// ****************************************************************************
// Application Initialization Logic
// ***************************************************************************/
init() {
// Setup graceful exit for SIGTERM and SIGINT
process.on('SIGTERM', this.handleSIGTERM.bind(this));
process.on('SIGINT', this.handleSIGINT.bind(this));
// Start Logging & Server
this.log.debug(config);
this.server = new Server(config, this.log);
this.server.init();
}
}
const app = new App();
app.init();