File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -1968,6 +1968,27 @@ async def healthcheck(db: Session = Depends(get_db)):
1968
1968
return {"status" : "healthy" }
1969
1969
1970
1970
1971
+ @app .get ("/ready" )
1972
+ async def readiness_check (db : Session = Depends (get_db )):
1973
+ """
1974
+ Perform a readiness check to verify if the application is ready to receive traffic.
1975
+
1976
+ Args:
1977
+ db: SQLAlchemy session dependency.
1978
+
1979
+ Returns:
1980
+ JSONResponse with status 200 if ready, 503 if not.
1981
+ """
1982
+ try :
1983
+ # Run the blocking DB check in a thread to avoid blocking the event loop
1984
+ await asyncio .to_thread (db .execute , text ("SELECT 1" ))
1985
+ return JSONResponse (content = {"status" : "ready" }, status_code = 200 )
1986
+ except Exception as e :
1987
+ error_message = f"Readiness check failed: { str (e )} "
1988
+ logger .error (error_message )
1989
+ return JSONResponse (content = {"status" : "not ready" , "error" : error_message }, status_code = 503 )
1990
+
1991
+
1971
1992
# Mount static files
1972
1993
# app.mount("/static", StaticFiles(directory=str(settings.static_dir)), name="static")
1973
1994
Original file line number Diff line number Diff line change @@ -47,6 +47,13 @@ def test_health_check(self, test_client):
47
47
assert response .status_code == 200
48
48
assert response .json ()["status" ] == "healthy"
49
49
50
+ def test_ready_check (self , test_client ):
51
+ """Test the readiness check endpoint."""
52
+ response = test_client .get ("/ready" )
53
+ # The readiness check returns 200 if DB is reachable
54
+ assert response .status_code == 200
55
+ assert response .json ()["status" ] == "ready"
56
+
50
57
def test_root_redirect (self , test_client ):
51
58
"""Test root path redirects to admin."""
52
59
response = test_client .get ("/" , allow_redirects = False )
You can’t perform that action at this time.
0 commit comments