-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
71 lines (58 loc) · 1.95 KB
/
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
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
/* eslint vars-on-top: "off" */
/* eslint no-process-exit: "off" */
/* eslint strict: "off" */
"use strict";
const _ = require("lodash");
const http2 = require("http2");
// http.globalAgent.maxSockets = 100; //obsolete in http/2
const config = require('./src/config');
let srcdir = null;
if(config.ENV === 'prod') {
srcdir = './build';
} else {
srcdir = './src';
require('babel-register');
}
const logger = require(`${srcdir}/logging`).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) {
return new Promise(function (resolve, reject) {
id = id || 'main';
const app = require(`${srcdir}/app`).default;
return app.ready.then(function () {
const 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`);
});
registerGracefulShutdown('SIGTERM', server, id);
registerGracefulShutdown('SIGINT', server, id);
resolve(server);
});
});
}
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);
}
}