Question

I'm working with Node and Redis, and I have a need to add a subscriber to redis in case an event happens. The problem is I only want the processing to happen once... And I'd really like for only need one of my forked processes to be listening to redis.

Just now I tested to see if I could fork a new cluster in another file and get it to start there, but no joy. Anyone know how to do this?

I am running the latest stable version of Node (0.6.15)

here is my code (2 files):


clusterTest1:
---
var c2 = require('./clusterTest2');

console.log('clusterTest1');
//c2.test();
---

clusterTest2: 
---
var cluster = require ('cluster');

console.log('clusterTest2');

if (cluster.isMaster) {
    cluster.fork();
} else {
    console.log('cluster spawned');
}
---

Output:

clusterTest2 clusterTest1 clusterTest2 cluster spawned clusterTest1

Thanks!

Was it helpful?

Solution

I found a workaround for this, using process.env.NODE_WORKER_ID. One can run different code depending on the NODE_WORKER_ID, which fixes my problem of only having one instance subscribe for messages.

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