0

I need to end all workers if my db fails to connect...

if (cluster.isMaster) {
    mongoose.connect(config.db.path, function(err) {
        if (err) {
            logger.error('Can\'t connect to database')
            // kill all workers here
            process.exit()
            return
        }
    })
}
Kin
  • 4,466
  • 13
  • 54
  • 106

2 Answers2

1

You might want to use cluster.disconnect()

Calls .disconnect() on each worker in cluster.workers.

When they are disconnected all internal handles will be closed, allowing the master process to die gracefully if no other event is waiting.

The method takes an optional callback argument which will be called when finished.

This can only be called from the master process.

There is also an optional argument passed to this function - callback <Function> which is called when all workers are disconnected and handles are closed.

oleh.meleshko
  • 4,675
  • 2
  • 27
  • 40
0

List all workers by using

cluster.workers

then use cluster.worker.kill() function to end them.

Shaikh Shahid
  • 1,185
  • 11
  • 13