-
-
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
This file contains 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