From af925bdff93e7f034689f5cd5f5af1be03efd015 Mon Sep 17 00:00:00 2001 From: Julee Gunjal Date: Tue, 22 Sep 2026 14:31:21 +0530 Subject: [PATCH] Fixes for the wrong status showing for gateway health and others is correct. --- lending-poc/Dockerfile | 2 +- lending-poc/gateway/main.py | 49 ++++++++++++++++++++++++--- lending-poc/scripts/start-combined.sh | 0 3 files changed, 45 insertions(+), 6 deletions(-) mode change 100644 => 100755 lending-poc/scripts/start-combined.sh diff --git a/lending-poc/Dockerfile b/lending-poc/Dockerfile index c9fd1b2..dded6b7 100644 --- a/lending-poc/Dockerfile +++ b/lending-poc/Dockerfile @@ -70,4 +70,4 @@ RUN chmod +x scripts/start-combined.sh EXPOSE 8000 8001 8002 8010 8080 -CMD ["scripts/start-combined.sh"] +CMD ["bash", "scripts/start-combined.sh"] diff --git a/lending-poc/gateway/main.py b/lending-poc/gateway/main.py index 101974b..f4cda58 100644 --- a/lending-poc/gateway/main.py +++ b/lending-poc/gateway/main.py @@ -132,6 +132,49 @@ async def create_case(request: Request) -> Response: # per-service request timeouts above, matching the aggregate /health below. HEALTH_PROXY_TIMEOUT_SECONDS = 5.0 +_HEALTHY_UPSTREAM_STATUSES = frozenset({"healthy", "ok"}) +_KNOWN_UNHEALTHY_UPSTREAM_STATUSES = frozenset( + {"unhealthy", "unreachable", "initializing", "degraded"} +) + + +def _status_from_upstream(resp: httpx.Response) -> str: + if resp.status_code >= 500: + try: + body_status = resp.json().get("status") + except ValueError: + body_status = None + if isinstance(body_status, str) and body_status: + if body_status in _HEALTHY_UPSTREAM_STATUSES: + return f"unhealthy ({resp.status_code})" + return body_status + return f"unhealthy ({resp.status_code})" + + if resp.status_code >= 400: + return f"unhealthy ({resp.status_code})" + + try: + body_status = resp.json().get("status") + except ValueError: + body_status = None + + if not isinstance(body_status, str) or not body_status: + return "unhealthy" + + if body_status in _HEALTHY_UPSTREAM_STATUSES: + return "healthy" + if body_status in _KNOWN_UNHEALTHY_UPSTREAM_STATUSES: + return body_status + return body_status + + +async def _probe_service(client: httpx.AsyncClient, base_url: str) -> str: + try: + resp = await client.get(f"{base_url}/health", timeout=HEALTH_PROXY_TIMEOUT_SECONDS) + except httpx.RequestError: + return "unreachable" + return _status_from_upstream(resp) + @app.get("/ocr/health") async def ocr_health(request: Request) -> Response: @@ -163,11 +206,7 @@ async def health(request: Request) -> dict: ("field_mapping", FIELD_MAPPING_BASE_URL), ("app", APP_BASE_URL), ): - try: - resp = await client.get(f"{base}/health", timeout=HEALTH_PROXY_TIMEOUT_SECONDS) - statuses[name] = "healthy" if resp.status_code == 200 else f"unhealthy ({resp.status_code})" - except httpx.RequestError: - statuses[name] = "unreachable" + statuses[name] = await _probe_service(client, base) overall = "healthy" if all(v == "healthy" for v in statuses.values()) else "degraded" return {"status": overall, "services": statuses} diff --git a/lending-poc/scripts/start-combined.sh b/lending-poc/scripts/start-combined.sh old mode 100644 new mode 100755