- 用事务 Outbox 和签名 Webhook 替换直接状态回调 - 增加 Outbox 投递 worker、重试与低频对账兜底 - 缓存客户领取页绑定状态并补充签名与迁移测试
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import type { Server } from 'node:http'
|
|
|
|
import { stopKuaishouIndustrySendCallbackRetryWorker } from '../services/platforms/kuaishou-industry/send-code-service.js'
|
|
import { stopIntegrationOutboxWorker } from '../services/integration/outbox-worker.js'
|
|
import { stopDouyuFulfillmentReconciler } from '../services/douyu-fulfillment/fulfillment-reconciler.js'
|
|
import { stopScheduledJobs } from '../services/scheduler/scheduler-service.js'
|
|
import { closeDb } from '../db/client.js'
|
|
import { logError, logInfo } from '../utils/logger.js'
|
|
import type { StartupState } from './state.js'
|
|
|
|
type ShutdownOptions = {
|
|
closeDatabase?: () => Promise<void>
|
|
}
|
|
|
|
export function createShutdownController(
|
|
server: Server,
|
|
startupState: StartupState,
|
|
{ closeDatabase = closeDb }: ShutdownOptions = {},
|
|
) {
|
|
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()
|
|
stopIntegrationOutboxWorker()
|
|
stopDouyuFulfillmentReconciler()
|
|
|
|
if (server.listening) {
|
|
await new Promise<void>((resolve) => {
|
|
server.close((error) => {
|
|
if (error) {
|
|
logError('[shutdown]', 'failed to close HTTP server', error)
|
|
process.exitCode = 1
|
|
}
|
|
|
|
resolve()
|
|
})
|
|
})
|
|
}
|
|
|
|
try {
|
|
await closeDatabase()
|
|
} catch (error) {
|
|
logError('[shutdown]', 'failed to close database pool', error)
|
|
process.exitCode = 1
|
|
}
|
|
}
|
|
|
|
return {
|
|
isShutdownStarted: () => shutdownStarted,
|
|
shutdown,
|
|
}
|
|
}
|