Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does bindListeners() has to be recalled when a worker dies? #9

Open
MickL opened this issue Sep 13, 2017 · 3 comments
Open

Does bindListeners() has to be recalled when a worker dies? #9

MickL opened this issue Sep 13, 2017 · 3 comments

Comments

@MickL
Copy link

MickL commented Sep 13, 2017

If yes, is it no problem to just call it again?

workers[i].on('exit', (code, signal) => {
    winstonCluster.bindListeners();
});
@ryankurte
Copy link
Owner

Yes the handler does have to be re-registered, no I think you probably shouldn't call it again.

Check out the relevant line here which will afaik cause multiple handlers to be registered to the same event on each existing thread.

If you wanted to remedy this, you could split it into a bindListener function which is called by the master on creation of all threads with bindListeners and also on the creation of a new thread following a dead worker. You'll also have to overwrite the logger in the new worker thread.

@MickL
Copy link
Author

MickL commented Sep 25, 2017

Could you provide an example?

My code is very default:

  // Get number of cpu-cores
  const num_processes = require('os').cpus().length;

  // Set worker file
  cluster.setupMaster({
    exec: './server/worker.ts'
  });

  // This stores our workers. We need to keep them to be able to reference
  // them based on source IP address. It's also useful for auto-restart, for example
  let workers = [];

  // Helper function for spawning worker at index 'i'
  let spawn = i => {
    logger.info(`Spawning worker ${i}`);
    workers[i] = cluster.fork();

    // Restart worker on exit
    workers[i].on('exit', (code, signal) => {
      if (signal) {
        logger.warn(`Worker was killed by signal: ${signal}`);
      } else if (code !== 0) {
        logger.warn(`Worker exited with error code: ${code}`);
      }
      spawn(i);
    });
  };

  // Spawn workers.
  for (let i = 0; i < num_processes; i++) {
    spawn(i);
  }

  // Bind winston to workers
  winstonCluster.bindListeners();

@ralphschuler
Copy link
Collaborator

In PR #13 there was a singe bindListener(worker, instance=null) function added to overcome that problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants