-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
87 lines (76 loc) · 2.03 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
// app.js
const __ = require('lodash');
const Server = require('./app/server.js');
const Logger = require('./app/lib/logger');
const config = require('./config/config');
/**
* Class representing the app
* @class App
*/
class App {
constructor(logger, isLambda) {
this.logger = {};
this.log = {};
config.isLambda = isLambda;
this.setupLogging(logger);
}
setupLogging(logger) {
if (!logger) {
this.logger = new Logger(config);
} else {
this.logger = logger;
}
this.log = this.logger.log;
}
// ****************************************************************************
// 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 (!__.isNull(this.server) && !__.isUndefined(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(isLambda) {
// 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(isLambda);
}
}
module.exports = App;