统一前后端代码格式化配置
This commit is contained in:
@@ -1,71 +1,70 @@
|
||||
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 { migrateJsonConfigFilesToDatabase } from "../services/config/json-config-migration-service.js";
|
||||
import { startKuaishouIndustrySendCallbackRetryWorker } from "../services/platforms/kuaishou-industry/send-code-service.js";
|
||||
import { startScheduledJobs } from "../services/scheduler/scheduler-service.js";
|
||||
import { logError, logInfo } from "../utils/logger.js";
|
||||
import { formatStartupError, type StartupState } from "./state.js";
|
||||
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 { migrateJsonConfigFilesToDatabase } from '../services/config/json-config-migration-service.js'
|
||||
import { startKuaishouIndustrySendCallbackRetryWorker } from '../services/platforms/kuaishou-industry/send-code-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;
|
||||
const CORE_BOOT_RETRY_DELAY_MS = 5_000
|
||||
|
||||
export async function bootstrapCoreServices(
|
||||
startupState: StartupState,
|
||||
isShutdownStarted: () => boolean
|
||||
isShutdownStarted: () => boolean,
|
||||
) {
|
||||
if (startupState.core.running || isShutdownStarted()) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
startupState.core.running = true;
|
||||
startupState.core.running = true
|
||||
|
||||
while (!isShutdownStarted() && !startupState.core.ready) {
|
||||
startupState.phase =
|
||||
startupState.core.attemptCount === 0 ? "starting" : "retrying";
|
||||
startupState.core.attemptCount += 1;
|
||||
startupState.core.lastAttemptAt = new Date().toISOString();
|
||||
startupState.phase = startupState.core.attemptCount === 0 ? 'starting' : 'retrying'
|
||||
startupState.core.attemptCount += 1
|
||||
startupState.core.lastAttemptAt = new Date().toISOString()
|
||||
|
||||
try {
|
||||
logInfo("[startup]", "开始执行快手核心启动步骤", {
|
||||
logInfo('[startup]', '开始执行快手核心启动步骤', {
|
||||
attempt: startupState.core.attemptCount,
|
||||
});
|
||||
})
|
||||
|
||||
await runDatabaseMigrations();
|
||||
await migrateJsonConfigFilesToDatabase();
|
||||
await ensureFulfillmentCatalogBootstrapped();
|
||||
await ensureAdminUsersBootstrapped();
|
||||
await runDatabaseMigrations()
|
||||
await migrateJsonConfigFilesToDatabase()
|
||||
await ensureFulfillmentCatalogBootstrapped()
|
||||
await ensureAdminUsersBootstrapped()
|
||||
|
||||
startupState.core.ready = true;
|
||||
startupState.core.lastError = "";
|
||||
startupState.phase = "ready";
|
||||
startupState.core.readyAt = new Date().toISOString();
|
||||
startupState.core.ready = true
|
||||
startupState.core.lastError = ''
|
||||
startupState.phase = 'ready'
|
||||
startupState.core.readyAt = new Date().toISOString()
|
||||
|
||||
logInfo("[startup]", "快手核心启动步骤完成,服务已就绪", {
|
||||
logInfo('[startup]', '快手核心启动步骤完成,服务已就绪', {
|
||||
attempt: startupState.core.attemptCount,
|
||||
});
|
||||
})
|
||||
|
||||
startScheduledJobs();
|
||||
startKuaishouIndustrySendCallbackRetryWorker();
|
||||
break;
|
||||
startScheduledJobs()
|
||||
startKuaishouIndustrySendCallbackRetryWorker()
|
||||
break
|
||||
} catch (error) {
|
||||
const message = formatStartupError(error);
|
||||
startupState.phase = "retrying";
|
||||
startupState.core.lastError = message;
|
||||
logError("[startup]", "快手核心启动步骤失败,将自动重试", {
|
||||
const message = formatStartupError(error)
|
||||
startupState.phase = 'retrying'
|
||||
startupState.core.lastError = message
|
||||
logError('[startup]', '快手核心启动步骤失败,将自动重试', {
|
||||
attempt: startupState.core.attemptCount,
|
||||
retryDelayMs: CORE_BOOT_RETRY_DELAY_MS,
|
||||
error,
|
||||
});
|
||||
})
|
||||
|
||||
await sleep(CORE_BOOT_RETRY_DELAY_MS);
|
||||
await sleep(CORE_BOOT_RETRY_DELAY_MS)
|
||||
}
|
||||
}
|
||||
|
||||
startupState.core.running = false;
|
||||
startupState.core.running = false
|
||||
}
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
setTimeout(resolve, ms)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,46 +1,43 @@
|
||||
import type { Server } from "node:http";
|
||||
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";
|
||||
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;
|
||||
export function createShutdownController(server: Server, startupState: StartupState) {
|
||||
let shutdownStarted = false
|
||||
|
||||
async function shutdown(signal: string) {
|
||||
if (shutdownStarted) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
shutdownStarted = true;
|
||||
startupState.phase = "shutting_down";
|
||||
logInfo("[shutdown]", `received ${signal}, closing HTTP server`);
|
||||
shutdownStarted = true
|
||||
startupState.phase = 'shutting_down'
|
||||
logInfo('[shutdown]', `received ${signal}, closing HTTP server`)
|
||||
|
||||
stopScheduledJobs();
|
||||
stopKuaishouIndustrySendCallbackRetryWorker();
|
||||
stopScheduledJobs()
|
||||
stopKuaishouIndustrySendCallbackRetryWorker()
|
||||
|
||||
if (!server.listening) {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
server.close((error) => {
|
||||
if (error) {
|
||||
logError("[shutdown]", "failed to close HTTP server", error);
|
||||
process.exitCode = 1;
|
||||
logError('[shutdown]', 'failed to close HTTP server', error)
|
||||
process.exitCode = 1
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
isShutdownStarted: () => shutdownStarted,
|
||||
shutdown,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,42 @@
|
||||
type StartupProcessError = {
|
||||
time: string;
|
||||
message: string;
|
||||
};
|
||||
time: string
|
||||
message: string
|
||||
}
|
||||
|
||||
export type StartupState = {
|
||||
phase: string;
|
||||
startedAt: string;
|
||||
phase: string
|
||||
startedAt: string
|
||||
core: {
|
||||
ready: boolean;
|
||||
running: boolean;
|
||||
attemptCount: number;
|
||||
readyAt: string;
|
||||
lastAttemptAt: string;
|
||||
lastError: string;
|
||||
};
|
||||
ready: boolean
|
||||
running: boolean
|
||||
attemptCount: number
|
||||
readyAt: string
|
||||
lastAttemptAt: string
|
||||
lastError: string
|
||||
}
|
||||
process: {
|
||||
lastUnhandledRejection: StartupProcessError | null;
|
||||
lastUncaughtException: StartupProcessError | null;
|
||||
};
|
||||
};
|
||||
lastUnhandledRejection: StartupProcessError | null
|
||||
lastUncaughtException: StartupProcessError | null
|
||||
}
|
||||
}
|
||||
|
||||
export function createStartupState(): StartupState {
|
||||
return {
|
||||
phase: "starting",
|
||||
phase: 'starting',
|
||||
startedAt: new Date().toISOString(),
|
||||
core: {
|
||||
ready: false,
|
||||
running: false,
|
||||
attemptCount: 0,
|
||||
readyAt: "",
|
||||
lastAttemptAt: "",
|
||||
lastError: "",
|
||||
readyAt: '',
|
||||
lastAttemptAt: '',
|
||||
lastError: '',
|
||||
},
|
||||
process: {
|
||||
lastUnhandledRejection: null,
|
||||
lastUncaughtException: null,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function buildHealthPayload(startupState: StartupState, shutdownStarted: boolean) {
|
||||
@@ -57,13 +57,13 @@ export function buildHealthPayload(startupState: StartupState, shutdownStarted:
|
||||
lastUnhandledRejection: startupState.process.lastUnhandledRejection,
|
||||
lastUncaughtException: startupState.process.lastUncaughtException,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function formatStartupError(error: unknown) {
|
||||
if (error instanceof Error && error.message) {
|
||||
return error.message.split("\n")[0]?.trim() || "未知错误";
|
||||
return error.message.split('\n')[0]?.trim() || '未知错误'
|
||||
}
|
||||
|
||||
return String(error || "未知错误").trim();
|
||||
return String(error || '未知错误').trim()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user