Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

cluster discussion: uniqueID and destroy #3007

Closed
AndreasMadsen opened this issue Mar 25, 2012 · 1 comment
Closed

cluster discussion: uniqueID and destroy #3007

AndreasMadsen opened this issue Mar 25, 2012 · 1 comment

Comments

@AndreasMadsen
Copy link
Member

The rewrite of cluster in 0.7 is basically done (or at least from my side). I have got a lot of emails over the time, many of them are about high level API that could be supported by 10 lines of userland code. But between them I think there are two valid subjects:

cluster.destroy([callback])

This is much like the cluster.disconnect([callback]) method, it destroy all workers (don't disconnect) and then close all server handlers. It could be implemented in userland with:

cluster.destroy = function (callback) {
  var map = Object.keys(cluster.workers);
  var missing = map.length;
  var close
  map.forEach(function (uniqueID) {
    var worker = cluster.workers[uniqueID];
    worker.on('death', function () {
      missing -= 1;

      // since no more workers exist cluster.disconnect 
      // won't disconnect any and immediately close all handlers
      if (missing === 0) cluster.disconnect(callback);
    }
    worker.destroy();
  });
};

So as you see it is quite easy to implement, however it isn't very obiouse. It do also give some API consistency: there is a worker.disconnect and a cluster.disconnect method, however there is only a worker.destroy method.

Worker = worker.restart() or worker.workerID

At some point a workerID was renamed to uniqueID because workerID was going to mean something else when using cluster.autoFork() (not implemented and will properly never be implemented). It was suposed to be an ID there started at 0
and increased to os.cpus().length (just like the LeanBoost cluster module).

However since this was never implemented uniqueID should be renamed back to workerID or a worker.restart() method should be implemented. When calling the worker.restart() method (assuming the worker is dead) the workerID will then be reused and uniqueID will increases.

This could also be implemented in userland, but is a little more complicated (so I won't write an example) because the env option in cluster.fork([env]) should be stored somewhere and then reused. When userland wan't to restart the worker.

With a worker.restart() method the unimplemented cluster.autoFork() cloud be implemented in userland this way:

cluster.autoFork = function () {
  var i = require('os').cpus().length;
  while(i--) cluster.fork();

  cluster.on('death', function (worker) {
    worker.restart();
  });
};

Note that I don't think userland should extend the cluster module object, but I think it made the examples more easy to understand.

@AndreasMadsen
Copy link
Member Author

Discussed a while ago.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant