优化订单流程与数据库关闭
This commit is contained in:
@@ -2,10 +2,19 @@ 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 { closeDb } from '../db/client.js'
|
||||
import { logError, logInfo } from '../utils/logger.js'
|
||||
import type { StartupState } from './state.js'
|
||||
|
||||
export function createShutdownController(server: Server, startupState: StartupState) {
|
||||
type ShutdownOptions = {
|
||||
closeDatabase?: () => Promise<void>
|
||||
}
|
||||
|
||||
export function createShutdownController(
|
||||
server: Server,
|
||||
startupState: StartupState,
|
||||
{ closeDatabase = closeDb }: ShutdownOptions = {},
|
||||
) {
|
||||
let shutdownStarted = false
|
||||
|
||||
async function shutdown(signal: string) {
|
||||
@@ -20,20 +29,25 @@ export function createShutdownController(server: Server, startupState: StartupSt
|
||||
stopScheduledJobs()
|
||||
stopKuaishouIndustrySendCallbackRetryWorker()
|
||||
|
||||
if (!server.listening) {
|
||||
return
|
||||
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()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user