@@ -668,11 +668,20 @@ export async function createApp(options = {}) {
668668 }
669669 }
670670
671+ let isShuttingDown = false ;
672+
671673 app . get ( '/health' , async ( _req , res ) => {
672674 const payload = await buildHealthPayload ( ) ;
673675 res . json ( payload ) ;
674676 } ) ;
675677
678+ app . get ( '/ready' , ( _req , res ) => {
679+ if ( isShuttingDown ) {
680+ return res . status ( 503 ) . json ( { status : 'shutting_down' , ready : false } ) ;
681+ }
682+ return res . json ( { status : 'ok' , ready : true } ) ;
683+ } ) ;
684+
676685 const siteOrigin =
677686 process . env . SITE_ORIGIN ?? allowedOrigins . find ( ( origin ) => origin !== '*' ) ?? '' ;
678687
@@ -786,6 +795,7 @@ export async function createApp(options = {}) {
786795 prefix : API_V1_PREFIX ,
787796 endpoints : {
788797 health : 'GET /health' ,
798+ ready : 'GET /ready' ,
789799 healthRpc : 'GET /health/rpc' ,
790800 metrics : 'GET /metrics' ,
791801 info : `GET ${ API_V1_PREFIX } ` ,
@@ -2116,6 +2126,11 @@ export async function createApp(options = {}) {
21162126 // Central error handler — must be registered after all routes
21172127 app . use ( errorHandler ) ;
21182128
2129+ app . _close = ( ) => {
2130+ isShuttingDown = true ;
2131+ try { dal . db . close ( ) ; } catch ( _ ) { }
2132+ } ;
2133+
21192134 return app ;
21202135}
21212136
@@ -2152,6 +2167,7 @@ export async function startServer(options = {}) {
21522167 async function gracefulShutdown ( signal ) {
21532168 if ( shuttingDown ) return ;
21542169 shuttingDown = true ;
2170+ app . _close ?. ( ) ;
21552171 log . info ( { signal, graceMs : SHUTDOWN_GRACE_MS } , 'graceful shutdown started' ) ;
21562172
21572173 const forceTimer = setTimeout ( ( ) => {
0 commit comments