Files
order_site/apps/backend/src/startup/shutdown.ts
T
2026-07-08 20:41:29 +08:00

47 lines
1.1 KiB
TypeScript

import type { Server } from "node:http";
import { stopKuaishouIndustrySendCallbackRetryWorker } from "../services/platforms/kuaishou-industry/send-code-service.js";
import { stopScheduledJobs } from "../services/scheduler/scheduler-service.js";
import { logError, logInfo } from "../utils/logger.js";
import type { StartupState } from "./state.js";
export function createShutdownController(
server: Server,
startupState: StartupState
) {
let shutdownStarted = false;
async function shutdown(signal: string) {
if (shutdownStarted) {
return;
}
shutdownStarted = true;
startupState.phase = "shutting_down";
logInfo("[shutdown]", `received ${signal}, closing HTTP server`);
stopScheduledJobs();
stopKuaishouIndustrySendCallbackRetryWorker();
if (!server.listening) {
return;
}
await new Promise<void>((resolve) => {
server.close((error) => {
if (error) {
logError("[shutdown]", "failed to close HTTP server", error);
process.exitCode = 1;
}
resolve();
});
});
}
return {
isShutdownStarted: () => shutdownStarted,
shutdown,
};
}