Skip to content
Snippets Groups Projects
Commit 92236153 authored by Gustavo Soviersovski's avatar Gustavo Soviersovski
Browse files

Merge branch 'feature_cluster' into 'development'

Cluster

See merge request !39
parents c0a65e0b f13fe9ad
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!39Cluster
Pipeline #
const debug = require('debug')('node-express-base'); const debug = require('debug')('simcaq-api');
const libs = `${process.cwd()}/libs`; const libs = `${process.cwd()}/libs`;
const config = require(`${libs}/config`); const config = require(`${libs}/config`);
const log = require(`${libs}/log`)(module); const log = require(`${libs}/log`)(module);
const app = require(`${libs}/app`); const app = require(`${libs}/app`);
const compatVersion = require(`${libs}/middlewares/checkVersion`); const compatVersion = require(`${libs}/middlewares/checkVersion`);
const cluster = require('cluster');
// Check if Node version is compatible // Check if Node version is compatible
if (!compatVersion()) { if (!compatVersion()) {
process.exit(1); process.exit(1);
} }
// Set default port: first environment variable PORT, then configuration and last 3000 if(cluster.isMaster) {
app.set('port', process.env.PORT || config.port || 3000); log.info(`Master ${process.pid} is running`);
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// Set default ip: first environment variable IOP, then configuration and last '127.0.0.1' const numCPUs = require('os').cpus().length;
app.set('ip', process.env.IP || config.ip || '127.0.0.1'); log.info(`Master will create ${numCPUs} workers`);
for(let i=0; i < numCPUs; ++i) {
const server = app.listen(app.get('port'), () => { cluster.fork();
log.info(`Express server listening on port ${server.address().port}`); }
});
// Caso uma instâcia morra
// For testing cluster.on('exit', (worker, code, signal) => {
module.exports = server; log.info(`Worker ${worker.process.pid} died`);
// Revive a instância
cluster.fork();
});
} else {
// Set default port: first environment variable PORT, then configuration and last 3000
app.set('port', process.env.PORT || config.port || 3000);
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// Set default ip: first environment variable IOP, then configuration and last '127.0.0.1'
app.set('ip', process.env.IP || config.ip || '127.0.0.1');
const server = app.listen(app.get('port'), () => {
log.info(`Express server listening on port ${server.address().port}`);
});
log.info(`Worker ${cluster.worker.id} is running (${process.pid})`);
// For testing
module.exports = server;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment