* Setup graceful shutdown handlers for SIGTERM and SIGINT
()
| 8675 | * Setup graceful shutdown handlers for SIGTERM and SIGINT |
| 8676 | */ |
| 8677 | private setupShutdownHandlers(): void { |
| 8678 | const gracefulShutdown = async (signal: string) => { |
| 8679 | logger.info({ signal }, 'Received shutdown signal, starting graceful shutdown'); |
| 8680 | await this.stop(); |
| 8681 | process.exit(0); |
| 8682 | }; |
| 8683 | |
| 8684 | process.on("SIGTERM", () => gracefulShutdown("SIGTERM")); |
| 8685 | process.on("SIGINT", () => gracefulShutdown("SIGINT")); |
| 8686 | |
| 8687 | process.on("uncaughtException", (err) => { |
| 8688 | logger.fatal({ err }, "Uncaught exception — shutting down"); |
| 8689 | // Give PostHog/OTel time to flush before exiting |
| 8690 | setTimeout(() => process.exit(1), 2000); |
| 8691 | }); |
| 8692 | |
| 8693 | process.on("unhandledRejection", (reason) => { |
| 8694 | logger.fatal( |
| 8695 | { err: reason instanceof Error ? reason : new Error(String(reason)) }, |
| 8696 | "Unhandled promise rejection" |
| 8697 | ); |
| 8698 | }); |
| 8699 | } |
| 8700 | |
| 8701 | /** |
| 8702 | * Stop the server gracefully |