拆分快手轻量后端入口

This commit is contained in:
yml
2026-05-25 20:42:27 +08:00
parent d58d483944
commit db22ff691b
23 changed files with 1146 additions and 13 deletions
@@ -0,0 +1,71 @@
import { runDatabaseMigrations } from "../db/migrate.js";
import { ensureAdminUsersBootstrapped } from "../services/admin/admin-auth-service.js";
import { ensureFulfillmentCatalogBootstrapped } from "../services/bootstrap/fulfillment-bootstrap-service.js";
import { startScheduledJobs } from "../services/scheduler/scheduler-service.js";
import { logError, logInfo } from "../utils/logger.js";
import { formatStartupError, type StartupState } from "./state.js";
const CORE_BOOT_RETRY_DELAY_MS = 5_000;
export async function bootstrapKuaishouCoreServices(
startupState: StartupState,
isShutdownStarted: () => boolean
) {
if (startupState.core.running || isShutdownStarted()) {
return;
}
startupState.core.running = true;
startupState.browser.status = "skipped";
startupState.browser.message = "kuaishou lite mode";
startupState.ocr.status = "skipped";
startupState.ocr.message = "kuaishou lite mode";
while (!isShutdownStarted() && !startupState.core.ready) {
startupState.phase =
startupState.core.attemptCount === 0 ? "starting" : "retrying";
startupState.core.attemptCount += 1;
startupState.core.lastAttemptAt = new Date().toISOString();
try {
logInfo("[startup:kuaishou]", "开始执行快手轻量核心启动步骤", {
attempt: startupState.core.attemptCount,
});
await runDatabaseMigrations();
await ensureFulfillmentCatalogBootstrapped();
await ensureAdminUsersBootstrapped();
startupState.core.ready = true;
startupState.core.lastError = "";
startupState.phase = "ready";
startupState.core.readyAt = new Date().toISOString();
logInfo("[startup:kuaishou]", "快手轻量核心启动步骤完成,服务已就绪", {
attempt: startupState.core.attemptCount,
});
startScheduledJobs();
break;
} catch (error) {
const message = formatStartupError(error);
startupState.phase = "retrying";
startupState.core.lastError = message;
logError("[startup:kuaishou]", "快手轻量核心启动步骤失败,将自动重试", {
attempt: startupState.core.attemptCount,
retryDelayMs: CORE_BOOT_RETRY_DELAY_MS,
error,
});
await sleep(CORE_BOOT_RETRY_DELAY_MS);
}
}
startupState.core.running = false;
}
function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
@@ -0,0 +1,44 @@
import type { Server } from "node:http";
import { stopScheduledJobs } from "../services/scheduler/scheduler-service.js";
import { logError, logInfo } from "../utils/logger.js";
import type { StartupState } from "./state.js";
export function createKuaishouShutdownController(
server: Server,
startupState: StartupState
) {
let shutdownStarted = false;
async function shutdown(signal: string) {
if (shutdownStarted) {
return;
}
shutdownStarted = true;
startupState.phase = "shutting_down";
logInfo("[shutdown:kuaishou]", `received ${signal}, closing HTTP server`);
stopScheduledJobs();
if (!server.listening) {
return;
}
await new Promise<void>((resolve) => {
server.close((error) => {
if (error) {
logError("[shutdown:kuaishou]", "failed to close HTTP server", error);
process.exitCode = 1;
}
resolve();
});
});
}
return {
isShutdownStarted: () => shutdownStarted,
shutdown,
};
}