feat: graceful exit handler

This commit is contained in:
Gergo Moricz 2024-07-09 14:29:32 +02:00
parent 914897c9d2
commit 77aa46588f

View File

@ -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();