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).