-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (27 loc) · 960 Bytes
/
index.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
var when = require('when');
var logger = require('./utility').logger;
var server = require('./server');
logger.info('Stock Analytics Server 0.1');
server.init()
.then(function() {
server.start();
})
.catch(function(error) {
logger.error('Exit with error: ' + JSON.stringify(error, null, 2));
});
function cleanup(options, exitCode) {
if (options.cleanup) {
server.stop();
}
if (exitCode || exitCode === 0) logger.error(exitCode);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', cleanup.bind(null, {cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', cleanup.bind(null, {exit:true}));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', cleanup.bind(null, {exit:true}));
process.on('SIGUSR2', cleanup.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', cleanup.bind(null, {exit:true}));