Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat: graceful exit handler

Gergo Moricz77aa46588f2e
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index ff1ee6d3c..747f8a7b8 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -26,10 +26,27 @@ if (cluster.isMaster) {
}
cluster.on("exit", (worker, code, signal) => {
- console.log(`Worker ${worker.process.pid} exited`);
- console.log("Starting a new worker");
- cluster.fork();
+ if (code !== null) {
+ console.log(`Worker ${worker.process.pid} exited`);
+ console.log("Starting a new worker");
+ cluster.fork();
+ }
});
+
+ const onExit = () => {
+ console.log("Shutting down gracefully...");
+
+ if (cluster.workers) {
+ for (const worker of Object.keys(cluster.workers || {})) {
+ cluster.workers[worker].process.kill();
+ }
+ }
+
+ process.exit();
+ };
+
+ process.on("SIGINT", onExit);
+ process.on("SIGTERM", onExit);
} else {
const app = express();