Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Try this and see what your output is (much simplified code just for debugging):

var cluster = require('cluster'),
    numCPUs = require('os').cpus().length;
console.log('global '+numCPUs);

// create the server 
if (cluster.isMaster) {
    console.log('master '+numCPUs);
    for (var i = 0; i < numCPUs; i++) {
        console.log('this is for CPU '+i);
        cluster.fork();
        console.log('just for giggles '+i);
    }
}
else {
    console.log('This is a worker!');
}

... presumably you should get the output from all of those calls. If everything is as expected, start adding your code back in bit by bit, see if it breaks and where, or if it starts working.

If you miss some output or something is unexpected, and you still don't know what to do next, post your results here.

Here's an example of expected output, from my dev server:

global 8
master 8
this is for CPU 0
just for giggles 0
this is for CPU 1
just for giggles 1
this is for CPU 2
just for giggles 2
this is for CPU 3
just for giggles 3
this is for CPU 4
just for giggles 4
this is for CPU 5
just for giggles 5
this is for CPU 6
just for giggles 6
this is for CPU 7
just for giggles 7
global 8
This is a worker!
global 8
This is a worker!
global 8
This is a worker!
global 8
global 8
This is a worker!
This is a worker!
global 8
This is a worker!
global 8
This is a worker!
global 8
This is a worker!

... which demonstrates a couple interesting things, actually, if you're not very familiar with cluster's API or just want to see it in action, specifically that cluster.fork() is an asynchronous call (i.e. it will not execute until the next loop, hence why you don't start seeing the workers announce themselves until after they've all been forked), but that child processes, once spawned, will execute in parallel (hence the doubling up of global 8 and This is a worker! messages in the middle).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top