3

I am working on a node project which I want to run it via node cluster. As I have some shared resources and configuration which can be used by each node cluster(Worker), At the start up I read these configuration and save it into redis db. In addition, in some other modules I need to query some external resource and fetch some data and save into redis db too(not at start up, when I need). For this reseaon, at the start up I put my configuration section into a function and call it if the cluster is Master(cluster.isMaster) :

if (cluster.isMaster) 
configure();

my question is how can I do the same in other modules? in other modules I need to query external resource and put into redis just in master cluster, since after that it will be available for worker clusters too. So, no need to query in all clusters.

So thanks in advance,

4

1 回答 1

2

From the api http://nodejs.org/api/cluster.html

cluster.isMaster# Boolean True if the process is a master. This is determined by the process.env.NODE_UNIQUE_ID. If process.env.NODE_UNIQUE_ID is undefined, then isMaster is true.

In you modules, you can either check require('cluster').isMaster, you should not check process.env.NODE_UNIQUE_ID directly, because it will always be removed after startup, always be undefined. https://github.com/nodejs/node/blob/16689e30aeff03c8c47a605425727cf190c169e9/lib/internal/bootstrap_node.js#L129

于 2014-01-25T07:59:06.297 回答