This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
56 lines (52 loc) · 2.25 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
50
51
52
53
54
55
56
module.exports = (function () {
'use strict';
// Temporarily use cluster. Should switch to Nginx for better performance per this article:
// https://medium.com/@fermads/node-js-process-load-balancing-comparing-cluster-iptables-and-nginx-6746aaf38272
const cluster = require('cluster');
const config = require('./server_config');
const logger = require('./lib/util/logger');
const cpuCount = config.isLocal() || config.isDevEnv() ? 1 : require('os').cpus().length;
function initServer() {
const http = require('http');
const events = require('events');
const express = require('express');
const server = express();
http.globalAgent.maxSockets = Infinity;
process.setMaxListeners(50);
events.EventEmitter.defaultMaxListeners = 50;
// Shared App Configuration settings
server.set('port', config.getConfig('SERVER_SETTINGS.PORT'));
require('./server_router')(server);
http.createServer(server).listen(server.get('port'), function () {
server.set('initialized', true);
server.emit('app:initialized');
logger.info('Express server listening on port ' + server.get('port'));
});
}
if (process.env.npm_package_engines_node !== undefined &&
process.env.npm_package_engines_node !== process.versions.node) {
logger.error('Current Node.js version is ' + process.versions.node +
'. app requires Node.js version: ' + process.env.npm_package_engines_node + '.');
process.exit();
}
if (cpuCount > 1 && cluster.isMaster) {
logger.info('CPU Count: ' + cpuCount);
cluster.setupMaster({
exec: 'server.js'
});
// Create a worker for each CPU
for (let i = 0; i < cpuCount; i++) {
logger.info('Spawning cluster worker: ' + i);
cluster.fork();
}
cluster.on('exit', function (worker) {
logger.error('Worker %s died. Restarting...', { worker_process_pid: worker.process.pid });
cluster.fork();
});
} else {
if (cluster.isWorker) {
logger.info('Starting Cluster Worker\'s AppServer: pid=' + cluster.worker.process.pid);
}
initServer();
}
})();