2026-06-02 00:11:29 +07:00
|
|
|
import type { Server } from "node:http";
|
2026-07-30 11:50:48 +07:00
|
|
|
import { createChildLogger } from "@/shared/logger/index";
|
2026-06-09 10:16:04 +07:00
|
|
|
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-08-05 23:58:30 +07:00
|
|
|
// Failsafe: graceful shutdown must never hang the process forever.
|
|
|
|
|
// httpServer.close() waits for ALL open connections (including lingering
|
|
|
|
|
// WebSocket/keep-alive sockets), so on a stuck connection the process would
|
|
|
|
|
// otherwise sit zombie and systemd (Restart=always) can never revive it.
|
|
|
|
|
const forceExitTimer = setTimeout(() => {
|
|
|
|
|
logger.error({ signal }, "Graceful shutdown timed out; forcing exit");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}, 10_000);
|
|
|
|
|
|
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-08-05 23:58:30 +07:00
|
|
|
clearTimeout(forceExitTimer);
|
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");
|
2026-08-05 23:58:30 +07:00
|
|
|
clearTimeout(forceExitTimer);
|
2026-06-09 16:56:44 +07:00
|
|
|
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();
|