fix(observabilidade): close DB no SIGTERM e distinguir ENOENT/parse errors

This commit is contained in:
2026-04-23 01:07:40 +01:00
parent e101577d61
commit b933b4c2e2
2 changed files with 22 additions and 1 deletions
+12 -1
View File
@@ -46,7 +46,18 @@ export function createSessionsRouter(db: SessionsDb): Router {
const { events } = await parseSessionFile(session.jsonl_path)
return res.json({ meta: session, events })
} catch (err) {
return res.status(500).json({ error: 'Failed to parse session', message: (err as Error).message })
const e = err as NodeJS.ErrnoException
if (e.code === 'ENOENT') {
return res.status(410).json({
error: 'Session file missing (stale index)',
session_id: parsed.data.id,
})
}
const isProduction = process.env.NODE_ENV === 'production'
return res.status(500).json({
error: 'Failed to parse session',
...(isProduction ? {} : { message: e.message }),
})
}
})
+10
View File
@@ -140,6 +140,16 @@ app.use('/api/operations', operationsRouter)
const sessionsDb = openSessionsDb(process.env.OBSERVABILIDADE_DB ?? DEFAULT_DB_PATH)
app.use('/api/sessions', createSessionsRouter(sessionsDb))
function closeSessionsDb(): void {
try {
sessionsDb.close()
} catch (err) {
console.error('[sessionsDb] erro ao fechar:', err)
}
}
process.on('SIGTERM', closeSessionsDb)
process.on('SIGINT', closeSessionsDb)
// Serve static files in production
if (isProduction) {
// __dirname is /app/api/dist, need to go up 2 levels to /app/dist