Created
May 7, 2018 14:27
-
-
Save edujr1/24165d1088a325e739e87557d6314af1 to your computer and use it in GitHub Desktop.
Clusterizar uma aplicação NodeJS de acordo com o numero máximo de processadores
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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