-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
49 lines (43 loc) · 1.38 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
const config = require('config');
const utils = require('./utils/utils');
const environmentManagerFactory = require('./environmentManager');
const environmentManager = environmentManagerFactory.getInstance();
const impressionManagerFactory = require('./listener/manager');
const app = require('./app');
const PORT = process.env.SPLIT_EVALUATOR_SERVER_PORT || 7548;
let server;
// Only available for in memory settings.
if (config.get('blockUntilReady')) {
environmentManager.ready().then(ready => spinUpServer(ready));
} else {
environmentManager.ready().then(ready => {
if (!ready) {
console.log('There is no client ready, initialization aborted');
process.exit(0);
}
});
spinUpServer(true);
}
function spinUpServer(splitClientsReady) {
if (!splitClientsReady) {
console.log('There is no client ready, initialization aborted');
return;
}
server = app.listen(PORT, '0.0.0.0', function () {
utils.uptime('init');
console.log('Server is Up and Running at Port : ' + PORT);
});
}
gracefulShutDown('SIGTERM');
gracefulShutDown('SIGINT');
function gracefulShutDown(signal) {
process.on(signal, async () => {
console.info(`${signal} signal received.`);
await environmentManagerFactory.destroy();
await impressionManagerFactory.destroy();
server.close(() => {
console.log('Http server closed.');
process.exit(0);
});
});
}