Skip to content

Commit 4e6000d

Browse files
authored
Merge pull request #61 from manavgup/feature/ready-endpoint
Add /ready endpoint for Kubernetes readiness checks with tests
2 parents dbd936b + 2bc2998 commit 4e6000d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

mcpgateway/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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
19721993
# app.mount("/static", StaticFiles(directory=str(settings.static_dir)), name="static")
19731994

tests/unit/mcpgateway/test_main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)