fix(observabilidade): close DB no SIGTERM e distinguir ENOENT/parse errors
This commit is contained in:
+12
-1
@@ -46,7 +46,18 @@ export function createSessionsRouter(db: SessionsDb): Router {
|
|||||||
const { events } = await parseSessionFile(session.jsonl_path)
|
const { events } = await parseSessionFile(session.jsonl_path)
|
||||||
return res.json({ meta: session, events })
|
return res.json({ meta: session, events })
|
||||||
} catch (err) {
|
} 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 }),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -140,6 +140,16 @@ app.use('/api/operations', operationsRouter)
|
|||||||
const sessionsDb = openSessionsDb(process.env.OBSERVABILIDADE_DB ?? DEFAULT_DB_PATH)
|
const sessionsDb = openSessionsDb(process.env.OBSERVABILIDADE_DB ?? DEFAULT_DB_PATH)
|
||||||
app.use('/api/sessions', createSessionsRouter(sessionsDb))
|
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
|
// Serve static files in production
|
||||||
if (isProduction) {
|
if (isProduction) {
|
||||||
// __dirname is /app/api/dist, need to go up 2 levels to /app/dist
|
// __dirname is /app/api/dist, need to go up 2 levels to /app/dist
|
||||||
|
|||||||
Reference in New Issue
Block a user