Skip to content

Instantly share code, notes, and snippets.

@WaleedAshraf
Last active October 31, 2018 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save WaleedAshraf/7c268c207b2af09f8dcd7275bd96cc80 to your computer and use it in GitHub Desktop.
Save WaleedAshraf/7c268c207b2af09f8dcd7275bd96cc80 to your computer and use it in GitHub Desktop.
Switching from cluster module to PM2 & RabbitMQ
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);
}
} else {
callback(cluster);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment