pull/715/head
Travis Fischer 2025-04-24 06:46:00 +07:00
rodzic 139f0695bf
commit 5fa7c5ae2d
2 zmienionych plików z 42 dodań i 13 usunięć

Wyświetl plik

@ -0,0 +1,40 @@
import { promisify } from 'node:util'
import type { ServerType } from '@hono/node-server'
import { asyncExitHook } from 'exit-hook'
import restoreCursor from 'restore-cursor'
import { db } from '@/db'
export function initExitHooks({
server,
timeoutMs = 10_000
}: {
server: ServerType
timeoutMs?: number
}) {
// Gracefully restore the cursor if run from a TTY
restoreCursor()
// Gracefully shutdown the HTTP server
asyncExitHook(
async function shutdownServerExitHook() {
await promisify(server.close)()
},
{
wait: timeoutMs
}
)
// Gracefully shutdown the postgres database connection
asyncExitHook(
async function shutdownDbExitHook() {
await db.$client.end({
timeout: timeoutMs
})
},
{
wait: timeoutMs
}
)
}

Wyświetl plik

@ -1,17 +1,13 @@
import { promisify } from 'node:util'
import { serve } from '@hono/node-server'
import { asyncExitHook } from 'exit-hook'
import { Hono } from 'hono'
import { compress } from 'hono/compress'
import { cors } from 'hono/cors'
import restoreCursor from 'restore-cursor'
import { apiV1 } from '@/api-v1'
import { env } from '@/lib/env'
import * as middleware from '@/lib/middleware'
restoreCursor()
import { initExitHooks } from './lib/exit-hooks'
export const app = new Hono()
@ -27,11 +23,4 @@ const server = serve({
port: env.PORT
})
asyncExitHook(
async () => {
await promisify(server.close)()
},
{
wait: 10_000
}
)
initExitHooks({ server })