2026-03-12 01:40:08 +00:00
|
|
|
"""HTTP health checker for OpsBridge."""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-03-12 11:33:16 +01:00
|
|
|
from dataclasses import dataclass
|
2026-03-12 01:40:08 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class HealthResult:
|
|
|
|
|
ok: bool
|
|
|
|
|
status_code: Optional[int] = None
|
|
|
|
|
error: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HealthChecker:
|
|
|
|
|
def __init__(self, url: str, timeout_seconds: int = 5):
|
|
|
|
|
self._url = url
|
|
|
|
|
self._timeout = timeout_seconds
|
|
|
|
|
|
|
|
|
|
async def check(self) -> HealthResult:
|
|
|
|
|
try:
|
|
|
|
|
async with httpx.AsyncClient(timeout=self._timeout) as client:
|
|
|
|
|
response = await client.get(self._url)
|
|
|
|
|
response.raise_for_status()
|
|
|
|
|
return HealthResult(ok=True, status_code=response.status_code)
|
|
|
|
|
except httpx.HTTPStatusError as e:
|
|
|
|
|
return HealthResult(ok=False, status_code=e.response.status_code, error=str(e))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return HealthResult(ok=False, error=str(e))
|