File size: 1,626 Bytes
9c4d9c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95af320
9c4d9c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Operational helpers for Hugging Face Space validation."""

from __future__ import annotations

import json
import os
from typing import Any, Dict

import httpx


def load_runtime_metadata() -> Dict[str, str]:
    """Normalize runtime metadata from local/HF environments."""
    return {
        "service": "missionctrl",
        "space_id": os.getenv("SPACE_ID", "unknown"),
        "container_id": os.getenv("HOSTNAME", "unknown"),
        "build_id": os.getenv("BUILD_ID", os.getenv("SPACE_ID", "unknown")),
        "git_sha": os.getenv("GIT_SHA", os.getenv("HF_SPACE_COMMIT_SHA", "unknown")),
    }


def smoke_check(base_url: str | None = None, timeout_s: float = 10.0) -> Dict[str, Any]:
    """Validate that health-critical endpoints respond with HTTP 200."""
    root = (base_url or os.getenv("ENV_BASE_URL", "http://127.0.0.1:7860")).rstrip("/")
    endpoints = ["/", "/health", "/state", "/logs"]
    results: Dict[str, Any] = {"base_url": root, "ok": True, "checks": []}

    with httpx.Client(timeout=timeout_s) as client:
        for endpoint in endpoints:
            url = f"{root}{endpoint}"
            response = client.get(url)
            is_ok = response.status_code == 200
            results["checks"].append({
                "endpoint": endpoint,
                "status_code": response.status_code,
                "ok": is_ok,
            })
            if not is_ok:
                results["ok"] = False
    return results


if __name__ == "__main__":
    report = {
        "runtime": load_runtime_metadata(),
        "smoke_check": smoke_check(),
    }
    print(json.dumps(report, indent=2))