4

I'm using node.js cluster module to create worker processes. And I set a custom variable in each worker's environment as I fork it.

I need to read that custom variable when a worker dies, but when a worker dies, I can't access its environment object anymore.

This is what I tried so far:

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

if (cluster.isMaster) {

    cluster.on('exit', function (worker, code, signal) {

        console.log('worker ' + worker.process.pid + ' died');

        var x = {
            workerId: worker.process.env.workerId // This is undefined.
        };
        cluster.fork(x);
    });

    for (var i = 0; i < os.cpus().length; i++) {
        var x = {
            workerId: i
        };
        cluster.fork(x);
    }

}
else {
    console.log("workerId: ", process.env.workerId);

    // simulate an exeption:
    throw "fakeError";

}

I know that's not gonna work, my question is: how to access to latest state of a worker's envoronment right before its death?

2
  • You can maintain the worker process ID to worker ID mapping in a object in the cluster code so that when a worker dies you can get the worker ID for the corresponding process ID.
    – Ankur
    Aug 31, 2015 at 8:14
  • When a process stops, its environment is gone. Perhaps you should explain what exactly it is that you want to do, there are probably better ways.
    – robertklep
    Aug 31, 2015 at 9:18

2 Answers 2

8

It seems that env only set in worker's process and is not accessible in master. Master only have primitive information about workers process. You can do what you want like:

// Fork workers.
for (var i = 0; i < numCPUs; i++) {
    var env = {workerId: i},
        newWorker = cluster.fork(env);
    newWorker.process.env = env;
}

cluster.on('exit', function (worker, code, signal) {
    console.log('worker ', worker.process.env.workerId, ' died');
    var env = worker.process.env,
        newWorker = cluster.fork(env);
    newWorker.process.env = env;
});

I hope it helps you. Whenever workers change their env, they should send message to master and inform it about those changes, so, master can update its information.

1
  • Trying this today, in cluster.on('exit'), worker.process.env is not defined. So I'm not seeing that as a way to pass information from a dead worker to the worker that is taking its place. Things could have changed in 8 years, I suppose.
    – Eric Hill
    Sep 20 at 18:55
-1

You can subscribe to the exit event of the cluster and you will be notified when any worker in the cluster dies. At that point you can do whatever you want.

Code Snippet picked directly from Documentation

cluster.on('exit', function(worker, code, signal) {
      console.log('worker %d died (%s). restarting...',
        worker.process.pid, signal || code);
      cluster.fork();
    });

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.