镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

worker.isOnline() ? #3294

Description

@sonnyp

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.

Activity

  1. sonnyp commented on Oct 9, 2015

    @sonnyp
    Author

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

  2. changed the title [-]worker.isOnline ?[/-] [+]worker.isOnline() ?[/+] on Oct 9, 2015
  3. added
    clusterIssues and PRs related to the cluster subsystem.
    on Oct 9, 2015
  4. bnoordhuis commented on Oct 10, 2015

    @bnoordhuis
    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.

  5. bnoordhuis commented on May 2, 2016

    @bnoordhuis
    Member

    Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clusterIssues and PRs related to the cluster subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions