doxout / recluster

Node clustering library with support for zero downtime reloading
522 stars 44 forks source link

Question: How to know when all workers are ready/active? #43

Closed mattbrunetti closed 7 years ago

mattbrunetti commented 7 years ago

We can listen for ready events on the cluster object, but these fire once for each worker.

How can we trigger some handler once all the workers are ready? Is there any such event or hook, public or private?

My use case is notifying PM2 that the cluster is ready, for graceful reloading. So this is somewhat related to #35 which is concerned about how to gracefully shut down a cluster.

I have this naive custom function to return a promise that resolves when all workers are ready, but I think with this there is the possibility that, before all workers are ready, a worker dies and gets replaced, and the promise will resolve too early:

function allWorkersReady (cluster) {
  return new Promise((resolve, reject) => {
    let pendingWorkersCount = cluster.workers().length
    cluster.on('ready', onReady)
    function onReady (worker) {
      if (--pendingWorkersCount === 0) {
        cluster.removeListener('ready', onReady)
        resolve()
      }
    }
  })
}
mattbrunetti commented 7 years ago

I was able to make PM2 clustering work for me, so I'm not using this package anymore, so I'll close this issue.

Thanks! :smile: