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)):
19681968 return {"status" : "healthy" }
19691969
19701970
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+
19711992# Mount static files
19721993app .mount ("/static" , StaticFiles (directory = str (settings .static_dir )), name = "static" )
19731994
Original file line number Diff line number Diff line change @@ -47,6 +47,13 @@ def test_health_check(self, test_client):
4747 assert response .status_code == 200
4848 assert response .json ()["status" ] == "healthy"
4949
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+
5057 def test_root_redirect (self , test_client ):
5158 """Test root path redirects to admin."""
5259 response = test_client .get ("/" , allow_redirects = False )
You can’t perform that action at this time.
0 commit comments