简体   繁体   中英

How to end all workers from master in NodeJS?

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
        }
    })
}

List all workers by using

cluster.workers

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM