Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sails with cluster (to use more cores)?

I am trying to launch sails.js application with cluster library to allow it so spawn more processes to use all cores of my machine but somehow the method used with express fails here.

var cluster = require('cluster'),
    numCPUs = require('os').cpus().length;

// create the server 
if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on('exit', function(worker, code, signal) {
    console.log('worker %d died (%s). restarting...', worker.process.pid, signal || code);
    cluster.fork();
  });
} else {
  // Start sails and pass it command line arguments
  require('sails').lift(require('optimist').argv);
  console.log("Sails forked");
}

After that I just normally run it via :

$ sails lift

but I get the same results when I run apache bench for this performance testing. Any help ? Am I missing something ?

EDIT

Even when I put console.log in cluster.fork if part then I do not get the output.

like image 255
Patryk Avatar asked Jan 28 '14 16:01

Patryk


1 Answers

Finally I have used pm2 module for this as mentioned in one of sails issues https://github.com/balderdashy/sails/issues/170

One should first install it:

$ npm install -g pm2

and then run the script:

$ pm2 start app.js -i max

then you can monitor it with:

$ pm2 monit

enter image description here

or list the processess:

$ pm2 l

enter image description here

like image 166
Patryk Avatar answered Sep 30 '22 14:09

Patryk