I'm building out a "manager" process with a set of workers. I'd like to put the workers into separate processes using the cluster (link) module. Since the worker's message processing may be fairly resource intensive it makes sense to push it to a different core.
Here's the pseudo code for the manager:
for i in worker_count
create a new process (fork = cluster.fork())
create a worker and pass in the process (fork) to its constructor
in the worker
when the worker gets a "work" chunk
it should be processed on a core that is not the same as the manager
I can't find much information on cluster that isn't just http.createServer etc. I can do this a different way but I'm wondering if there's a way to grab the "context" of the cluster and put a function inside that context.
When you fork the cluster, it duplicates the current process entirely. There is one 'master' worker and the rest are all created as child processes. One way to pass work to the child workers is to send messages:
if (cluster.isMaster) {
// Spawn all your child workers here
var worker = cluster.fork();
// You could send a message to the worker from here if you wanted
worker.send('some message');
// You could loop through all the workers and send messages to them
for (var id in cluster.workers) {
cluster.workers[id].send('some message');
}
} else {
// This is where your child workers will branch into as they are not the master process
process.on('message', function (msg) {
if (msg === "some message") {
doSomeStuff();
}
});
}