-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (53 loc) · 1.98 KB
/
server.js
File metadata and controls
64 lines (53 loc) · 1.98 KB
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
'use strict';
const config = require('config');
const stoppable = require('stoppable');
const { getApp } = require('./server/app');
const { getContext } = require('./context');
const { logger } = require('./logger');
const { getWorkers } = require('./workers/worker-client');
const { getDB } = require('./data/db-client');
const killSignals = {
SIGHUP: 1,
SIGINT: 2,
SIGUSR2: 12,
SIGTERM: 15,
};
function shutdown(nodeApp, context, signal, value) {
context.logger.info(`Trying shutdown, got signal ${signal}`);
nodeApp.stop(() => {
context.logger.info('Node app stopped.');
context.db.$pool.end();
context.logger.info('DB connections stopped.');
process.exit(128 + value);
});
}
function start(context) {
const app = getApp(context);
const nodeApp = stoppable(app.listen(config.PORT, () => context.logger.info(`Listening on port ${config.PORT}!`)));
nodeApp.timeout = 0;
nodeApp.keepAliveTimeout = 60000; // 60 secs
process.on('unhandledRejection', (error, promise) => {
context.logger.error(`unhandledRejection at: ${promise} `, {
stack: error.stack,
error: JSON.stringify(error),
});
});
process.on('uncaughtException', error => {
context.logger.error(`uncaughtException: ${error.message}`, {
stack: error.stack,
error: JSON.stringify(error),
});
});
process.once('SIGUSR2', () => shutdown(nodeApp, context, 'SIGUSR2', killSignals.SIGUSR2));
process.once('SIGHUP', () => shutdown(nodeApp, context, 'SIGHUP', killSignals.SIGHUP));
process.once('SIGINT', () => shutdown(nodeApp, context, 'SIGINT', killSignals.SIGINT));
process.once('SIGTERM', () => shutdown(nodeApp, context, 'SIGTERM', killSignals.SIGTERM));
return nodeApp;
}
// First get context that waits for the DB to be available before starting the web server
getContext(config, logger, getDB(config.PG_CONNECTION), getWorkers(config))
.then(start)
.catch(error => {
logger.info(`Failed to start services: ${error.stack}`);
process.exit(1);
});