Monday, 15 May 2017

Node.js child process worker not emitting disconnect event when disconnecting

I am using the Cluster module to create child workers. Within the child workers I want an event handler for the disconnect event. However, when I call the process.disconnect() function, the disconnect event is not thrown. Example code below: Each worker should log it has disconnected, but it does not. I am running node 6.10.

const cluster = require('cluster');

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  for (let i = 0; i < 4; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {
  console.log(`Worker ${process.pid} started`);
  process.on('disconnect', () => console.log(`Worker ${process.pid} disconnected`) );
  setTimeout( () => process.disconnect(), 3000 );
}



via Josh

No comments:

Post a Comment