feat: Improve stackchain-dashboard health and reliability #36
21
src/main.py
21
src/main.py
|
|
@ -30,6 +30,27 @@ def health() -> dict[str, str]:
|
||||||
return {"status": "ok", "service": "stackchain-dashboard"}
|
return {"status": "ok", "service": "stackchain-dashboard"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/readyz")
|
||||||
|
async def readiness():
|
||||||
|
"""Return readiness after verifying the configured Gitea connection."""
|
||||||
|
try:
|
||||||
|
user = await current_user()
|
||||||
|
except Exception as exc:
|
||||||
|
return JSONResponse(
|
||||||
|
{
|
||||||
|
"status": "not_ready",
|
||||||
|
"service": "stackchain-dashboard",
|
||||||
|
"error": str(exc),
|
||||||
|
},
|
||||||
|
status_code=503,
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"status": "ready",
|
||||||
|
"service": "stackchain-dashboard",
|
||||||
|
"gitea_user": user["login"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/v1/context")
|
@app.get("/api/v1/context")
|
||||||
async def context() -> JSONResponse:
|
async def context() -> JSONResponse:
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,39 @@
|
||||||
from src.main import app, health
|
import pytest
|
||||||
|
|
||||||
|
from src import main
|
||||||
|
|
||||||
|
|
||||||
def test_health_endpoint_reports_service_liveness_without_gitea_access():
|
def test_health_endpoint_reports_service_liveness_without_gitea_access():
|
||||||
assert any(getattr(route, "path", None) == "/healthz" for route in app.routes)
|
assert any(getattr(route, "path", None) == "/healthz" for route in main.app.routes)
|
||||||
assert health() == {"status": "ok", "service": "stackchain-dashboard"}
|
assert main.health() == {"status": "ok", "service": "stackchain-dashboard"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_readiness_endpoint_reports_connected_gitea_user(monkeypatch):
|
||||||
|
async def connected_user():
|
||||||
|
return {"login": "timmy"}
|
||||||
|
|
||||||
|
monkeypatch.setattr(main, "current_user", connected_user)
|
||||||
|
|
||||||
|
response = await main.readiness()
|
||||||
|
|
||||||
|
assert any(getattr(route, "path", None) == "/readyz" for route in main.app.routes)
|
||||||
|
assert response == {
|
||||||
|
"status": "ready",
|
||||||
|
"service": "stackchain-dashboard",
|
||||||
|
"gitea_user": "timmy",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_readiness_endpoint_returns_503_when_gitea_is_unavailable(monkeypatch):
|
||||||
|
async def unavailable_user():
|
||||||
|
raise RuntimeError("connection refused")
|
||||||
|
|
||||||
|
monkeypatch.setattr(main, "current_user", unavailable_user)
|
||||||
|
|
||||||
|
response = await main.readiness()
|
||||||
|
|
||||||
|
assert response.status_code == 503
|
||||||
|
assert b'"status":"not_ready"' in response.body
|
||||||
|
assert b'"error":"connection refused"' in response.body
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user