12

I pass arguments when create chlid-processes

if (cluster.isMaster) {
    for (var i = 0; i < os.cpus().length; i++) {
        var new_worker_env = {};
        new_worker_env["WORKER_NAME"] = "worker" + i;

        var new_worker = cluster.fork(new_worker_env);
    }
}

and then try to read it in childs:

if ( process.env["WORKER_NAME"] != undefined ) instance.name = process.env["WORKER_NAME"];

but this var isn't exist, why?

Node v0.8.8

1 Answer 1

19

Seems to work for me on Windows, Node.js version 0.8.8

var cluster = require('cluster'),
    os      = require('os');

if (cluster.isMaster) {
    for (var i = 0; i < os.cpus().length; i++) {
        var new_worker_env = {};

        new_worker_env["WORKER_NAME"] = "worker" + i;

        var new_worker = cluster.fork(new_worker_env);
    }
} else {
    console.log(process.env['WORKER_NAME']);
}

outputs:

worker0
worker1
Sign up to request clarification or add additional context in comments.

3 Comments

But for me (I've made new app same as yours) it doesn't work, output is eight "undefined". What it may be? I work under Unix x64 (freebsd)
I found reason, when I use this line of code it doesn't work, why?: var server_store = new (require("socket.io-clusterhub"));
Its because the socket.io-clusterhub dependency clusterhub, github.com/fent/clusterhub, overwrites the cluster.fork method and does not pass the env to the original method. A workaround would be to edit /node_modules/clusterhub/lib/fork.js where it over-rides the cluster.fork method to pass the env or file a bug report with the clusterhub project above and wait for them to fix it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.