Skip to content

Instantly share code, notes, and snippets.

@hsingh23
Forked from edujr1/app.js
Created May 7, 2018 14:30
Show Gist options
  • Save hsingh23/8e14dad7e648e3c3e1a0f382bba47778 to your computer and use it in GitHub Desktop.
Save hsingh23/8e14dad7e648e3c3e1a0f382bba47778 to your computer and use it in GitHub Desktop.
Clusterizar uma aplicação NodeJS de acordo com o numero máximo de processadores
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const express = require('express');
const app = express();
module.exports = app;
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died with code: ${code}, and signal: ${signal}`);
console.log('Starting a new worker');
cluster.fork();
});
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
var porta = 8090;
app.listen(porta, function () {
console.log('Servidor' + (cluster.isMaster ? ' master ' : ' child ') + 'Online em localhost:' + porta);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment