Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker.isOnline() ? #3294

Closed
sonnyp opened this issue Oct 9, 2015 · 3 comments
Closed

worker.isOnline() ? #3294

sonnyp opened this issue Oct 9, 2015 · 3 comments
Labels
cluster Issues and PRs related to the cluster subsystem.

Comments

@sonnyp
Copy link

sonnyp commented Oct 9, 2015

I have a piece of code that needs to handle present and future workers once at any point during the application life time.

Here is a simple test case

var cluster = require('cluster')

var handleWorker = function(worker) {
  console.log(worker.id)
}

if (cluster.isMaster) {
  for (var i = 0; i < 2; i++) {
    cluster.fork()
  }
  for (var k in cluster.workers) {
    var worker = cluster.workers[k]
    if (worker.isConnected()) handleWorker(worker)
  }
  cluster.on('online', handleWorker)
}

This code outputs:

1
2
2
1

The intent of the piece of code is to execute something once for each present and future online worker. If we had a isOnline method and I could use it instead of isConnected the code would output:

1
2

Or maybe there is already a way around this?

This is definitively a minor issue/use case; I'm working around it by keeping a list of handled workers but it would be nice if I didn't had to.

Would isOnline be a good solution to the problem? If so I'd be happy to send a PR.

@sonnyp
Copy link
Author

sonnyp commented Oct 9, 2015

BTW there is the worker.state property that is set to 'online' once the worker is online but it's not a documented feature

@sonnyp sonnyp changed the title worker.isOnline ? worker.isOnline() ? Oct 9, 2015
@silverwind silverwind added the cluster Issues and PRs related to the cluster subsystem. label Oct 9, 2015
@bnoordhuis
Copy link
Member

In the example you posted an .isOnline property or function wouldn't make a difference. Inside the loop it's always going to be false because 'online' is an asynchronous event.

It might make sense if you have something in there that introduces a discontinuity, like setTimeout() or setImmediate(). Maybe try your hand at a pull request and see what the reactions are.

@bnoordhuis
Copy link
Member

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cluster Issues and PRs related to the cluster subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants