Try this and see what your result is (very simplified code 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 an output from all these calls. If everything is as expected, start adding your code back in half, see if it breaks and where, or if it starts to work.
If you miss a conclusion or something unexpected, and you still don't know what to do next, post your results here.
Here is an example of the 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 shows a couple of interesting things, in fact, if you are not very familiar with the cluster API or just want to see it in action, in particular that cluster.fork() is an asynchronous call (i.e. it will not be executed until the next cycle, therefore, why you do not start to see how the workers declare themselves until they are all bifurcated), but this child process, once generated, will be executed in parallel (therefore, doubling numbers global 8 and This is a worker! messages in the middle).
Jason source share