hunterloftis / throng

A simple worker-manager for clustered Node.js apps
880 stars 50 forks source link

Exit master once commenced shutdown and all workers exited #55

Open ikonst opened 3 months ago

ikonst commented 3 months ago

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 commented 1 month ago

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 commented 1 month ago

... when the primary process disconnects.

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