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

Exit master once commenced shutdown and all workers exited #55

Open
ikonst opened this issue Jul 11, 2024 · 2 comments
Open

Exit master once commenced shutdown and all workers exited #55

ikonst opened this issue Jul 11, 2024 · 2 comments

Comments

@ikonst
Copy link

ikonst commented Jul 11, 2024

Currently we're waiting for the grace timeout regardless of the workers terminating.

Something like this:

  function shutdown(signal) {
    return () => {
      running = false;
      setTimeout(() => forceKill(signal), config.grace).unref();
+     cluster.on("disconnect", () => {
+       if (
+         Object.values(cluster.workers ?? {}).every(
+           (w) => w?.isDead() || !w?.isConnected(),
+         )
+       ) {
+         // All workers have exited or disconnected
+         process.exit();
+       }
+     });
@ramipellumbi
Copy link

This approach doesn't quite work since not all workers may be dead when the primary process disconnects. You actually need to effectively wait for workers to exit before exiting the primary process. I spawned a library based on throng that I was using for my own application development for a while that handles this case -- https://www.npmjs.com/package/thart?activeTab=readme

Specifically, see here https://github.com/ramipellumbi/thart/blob/f5ce0b17df80dbae8054e28efa3782bdb5063b8c/src/primary.ts#L62

@ikonst
Copy link
Author

ikonst commented Sep 27, 2024

... when the primary process disconnects.

You mean, when the worker disconnects from the master process?

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

2 participants