Implement asynchronous database ping for health checks (#24380)
This commit is contained in:
1 parent
1f977d072e
commit
2a18dc98ac
1 file changed
+10
-2
@@ -2812,6 +2812,14 @@ async def get_opensearch_xml():
|
|||||||
return Response(content=xml_content, media_type='application/xml')
|
return Response(content=xml_content, media_type='application/xml')
|
||||||
|
|
||||||
|
|
||||||
|
def _sync_db_ping() -> None:
|
||||||
|
ScopedSession.execute(text('SELECT 1;')).all()
|
||||||
|
|
||||||
|
|
||||||
|
async def async_db_ping() -> None:
|
||||||
|
await asyncio.to_thread(_sync_db_ping)
|
||||||
|
|
||||||
|
|
||||||
@app.get('/health')
|
@app.get('/health')
|
||||||
async def healthcheck():
|
async def healthcheck():
|
||||||
return {'status': True}
|
return {'status': True}
|
||||||
@@ -2833,7 +2841,7 @@ async def readiness_check():
|
|||||||
|
|
||||||
# Check database connectivity
|
# Check database connectivity
|
||||||
try:
|
try:
|
||||||
ScopedSession.execute(text('SELECT 1;')).all()
|
await async_db_ping()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning(f'Readiness check DB ping failed: {e!r}')
|
log.warning(f'Readiness check DB ping failed: {e!r}')
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@@ -2860,7 +2868,7 @@ async def readiness_check():
|
|||||||
|
|
||||||
@app.get('/health/db')
|
@app.get('/health/db')
|
||||||
async def healthcheck_with_db():
|
async def healthcheck_with_db():
|
||||||
ScopedSession.execute(text('SELECT 1;')).all()
|
await async_db_ping()
|
||||||
return {'status': True}
|
return {'status': True}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user