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()
}
}
})
}
We can listen for
ready
events on thecluster
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: