feat(system): implement graceful shutdown and expand websocket events

Improve system reliability and real-time capabilities by implementing a robust lifecycle management system and adding new broadcast events for attachments and voice recordings.

- Implement asynchronous graceful shutdown in backend to close HTTP, WebSocket, Redis, and database connections.
- Add new WebSocket broadcast events: `attachment_created`, `voice_recording_started`, `voice_recording_stopped`, `voice_recording_uploaded`, and `analysis_queue_status`.
- Refactor media status handling to use boolean `playing` state instead of string-based status.
- Centralize `PageResult` and `VoiceRecording` types to improve consistency between frontend and backend.
- Update frontend API client to include `listRecordings` and handle new WebSocket event types.
- Fix type mismatches in voice command handling and media status reporting.
This commit is contained in:
MythEclipse
2026-06-09 16:56:44 +07:00
parent 3a7b005d95
commit 7108f6bb47
14 changed files with 161 additions and 90 deletions
+39 -14
View File
@@ -1,6 +1,10 @@
import type { Server } from "node:http";
import { createChildLogger } from "@bete/shared/logger";
import { startHttpServer } from "./http/server.js";
import { closeDatabase } from "./shared/database/index.js";
import { stopCommandBridge } from "./shared/redis/index.js";
import { stopRedisBridge as stopEventBridge } from "./ws/redis-bridge.js";
import { closeWebSocketServer } from "./ws/server.js";
const logger = createChildLogger("backend");
@@ -17,22 +21,43 @@ async function main() {
}
}
function shutdown(signal: string) {
async function shutdown(signal: string) {
logger.info({ signal }, "Shutting down gracefully");
if (httpServer) {
httpServer.close(() => {
logger.info("HTTP server closed");
process.exit(0);
});
try {
// 1. Stop accepting new HTTP connections
if (httpServer) {
await new Promise<void>((resolve) => {
httpServer!.close(() => {
logger.info("HTTP server closed");
resolve();
});
});
}
// Force exit after 10s if connections don't close
setTimeout(() => {
logger.error("Forced shutdown after timeout");
process.exit(1);
}, 10_000).unref();
} else {
// 2. Close WebSocket server
closeWebSocketServer();
// 3. Stop Redis bridges (event subscriptions + command channel)
await Promise.allSettled([
stopEventBridge().catch((err) =>
logger.warn({ err }, "Error stopping event bridge"),
),
stopCommandBridge().catch((err) =>
logger.warn({ err }, "Error stopping command bridge"),
),
]);
// 4. Close database pool
await closeDatabase().catch((err) =>
logger.warn({ err }, "Error closing database"),
);
logger.info("Graceful shutdown completed");
process.exit(0);
} catch (err) {
logger.error({ err }, "Error during graceful shutdown");
process.exit(1);
}
}
@@ -41,12 +66,12 @@ process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("uncaughtException", (err) => {
logger.error({ err }, "Uncaught exception");
process.exit(1);
shutdown("uncaughtException");
});
process.on("unhandledRejection", (reason) => {
logger.error({ reason }, "Unhandled rejection");
process.exit(1);
shutdown("unhandledRejection");
});
main();