Last active
March 25, 2018 13:21
-
-
Save WaleedAshraf/e50cb3965da33c0507db656e1423f7ce to your computer and use it in GitHub Desktop.
Switching from cluster module to PM2 & RabbitMQ
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
var cluster = require('cluster'); | |
const numCPUs = require('os').cpus().length; | |
module.exports.create = function(options, callback){ | |
if (cluster.isMaster) { | |
// fork child process for notif/sms/email worker | |
global.smsWorker = require('child_process').fork('./smsWorker'); | |
global.emailWorker = require('child_process').fork('./emailWorker'); | |
global.notifiWorker = require('child_process').fork('./notifWorker'); | |
// fork application workers | |
for (var i = 0; i < numCPUs; i++) { | |
var worker = cluster.fork().process; | |
console.log('worker started. process id %s', worker.pid); | |
} | |
// if application worker gets disconnected, start new one. | |
cluster.on('disconnect', function (worker) { | |
console.error('Worker disconnect: ' + worker.id); | |
var newWorker = cluster.fork().process; | |
console.log('Worker started. Process id %s', newWorker.pid); | |
}); | |
} else { | |
callback(cluster); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment