-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
103 lines (86 loc) · 2.96 KB
/
index.js
File metadata and controls
103 lines (86 loc) · 2.96 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
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
/* eslint vars-on-top: "off" */
/* eslint no-process-exit: "off" */
/* eslint strict: "off" */
"use strict";
const config = require('./src/config');
const _ = require("lodash");
const http = require("http");
const http2 = require("http2");
if (config.HTTP_VERSION === '1')
http.globalAgent.maxSockets = 100; //obsolete in http/2
let srcdir = null;
if(config.ENV === 'prod') {
srcdir = './build';
} else {
srcdir = './src';
require('babel-register');
}
const loggingmod = require(`${srcdir}/logging`);
const logger = loggingmod.default;
logger.info("BEGIN LOGGING - SEVERITY = %s", config.LOGGER_LEVEL);
logger.info("Current environment: %s", config.ENV)
function registerGracefulShutdown(signal, server, id) {
process.on(signal, function() {
logger.info(`Server(${id}) received signal ${signal}, attempt exit`);
server.close(function() {
logger.info(`Server(${id}) finished closing, exiting`);
process.exit(0);
});
});
}
function startThisProcess(id) {
const appmod = require(`${srcdir}/app`);
const app = appmod.default;
return appmod.checkAppPrerequisitesAsync().then(
() => {
logger.info('App prerequisites check passed');
id = id || 'main';
return app.ready.then(function () {
let server;
if (config.HTTP_VERSION === '2') {
server = http2.createServer(app.callback()); // Create an HTTP/2 server without SSL (SSL configured on the proxy)
server.listen(config.port, function () {
logger.info(`Server(${id}) listening on port ${config.port} with HTTP/2`);
});
} else {
server = app.listen(config.port, function() {
logger.info(`Server(${id}) listening on port ${config.port}`);
});
}
registerGracefulShutdown('SIGTERM', server, id);
registerGracefulShutdown('SIGINT', server, id);
return server;
});
},
(reason) => {
logger.error('App prerequisites check failed');
loggingmod.exitAfterFlushAndWait(1, 1000);
throw new Error(reason);
}
);
}
if(config.ENV === "test" || config.ENV === "development" || !config.CLUSTER) {
startThisProcess();
} else {
var cluster = require('cluster');
if(cluster.isMaster) {
// Fork workers.
_.times(config.CLUSTER_WORKERS, function() { cluster.fork(); });
cluster.on('exit', function(deadWorker, code, signal) {
logger.warn(`Server(${deadWorker.process.pid}) died.`);
// Restart the worker
cluster.fork();
});
} else {
startThisProcess(cluster.worker.process.pid);
}
}
/* If you're ever wondering why the application does not end on error:
* (1) The version of Node.js in use might still allow unhandled promises
* (2) There are open handles that Node.js does not want to close prematurely
* (uncomment code below to see them)
*/
//setInterval(() => {
// console.debug('ActiveRequests:', process._getActiveRequests());
// console.debug('ActiveHandles:', process._getActiveHandles());
//}, 2000);