2026-06-02 00:11:29 +07:00
|
|
|
import type { Server } from "node:http";
|
2026-06-09 10:16:04 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
|
|
|
|
import { startHttpServer } from "./http/server.js";
|
2026-06-09 16:56:44 +07:00
|
|
|
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";
|
2026-06-01 21:44:29 +07:00
|
|
|
|
|
|
|
|
const logger = createChildLogger("backend");
|
|
|
|
|
|
2026-06-02 00:11:29 +07:00
|
|
|
let httpServer: Server | undefined;
|
|
|
|
|
|
2026-06-01 21:44:29 +07:00
|
|
|
async function main() {
|
|
|
|
|
try {
|
|
|
|
|
logger.info("Starting Discord Moderation Backend Service");
|
2026-06-02 00:11:29 +07:00
|
|
|
httpServer = await startHttpServer();
|
2026-06-01 21:44:29 +07:00
|
|
|
logger.info("Backend service ready");
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error({ err }, "Failed to start backend service");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 16:56:44 +07:00
|
|
|
async function shutdown(signal: string) {
|
2026-06-02 00:11:29 +07:00
|
|
|
logger.info({ signal }, "Shutting down gracefully");
|
2026-06-01 21:44:29 +07:00
|
|
|
|
2026-06-09 16:56:44 +07:00
|
|
|
try {
|
|
|
|
|
// 1. Stop accepting new HTTP connections
|
|
|
|
|
if (httpServer) {
|
|
|
|
|
await new Promise<void>((resolve) => {
|
2026-07-26 14:27:36 +07:00
|
|
|
httpServer?.close(() => {
|
2026-06-09 16:56:44 +07:00
|
|
|
logger.info("HTTP server closed");
|
|
|
|
|
resolve();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-09 16:56:44 +07:00
|
|
|
// 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");
|
2026-06-02 00:11:29 +07:00
|
|
|
process.exit(0);
|
2026-06-09 16:56:44 +07:00
|
|
|
} catch (err) {
|
|
|
|
|
logger.error({ err }, "Error during graceful shutdown");
|
|
|
|
|
process.exit(1);
|
2026-06-02 00:11:29 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
|
|
|
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
2026-06-01 21:44:29 +07:00
|
|
|
|
|
|
|
|
process.on("uncaughtException", (err) => {
|
|
|
|
|
logger.error({ err }, "Uncaught exception");
|
2026-06-09 16:56:44 +07:00
|
|
|
shutdown("uncaughtException");
|
2026-06-01 21:44:29 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process.on("unhandledRejection", (reason) => {
|
|
|
|
|
logger.error({ reason }, "Unhandled rejection");
|
2026-06-09 16:56:44 +07:00
|
|
|
shutdown("unhandledRejection");
|
2026-06-01 21:44:29 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
main();
|