Skip to content

worker.isOnline() ?Β #3294

@sonnyp

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clusterIssues and PRs related to the cluster subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions