Download check_server.py from scomb2/Michel: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/scomb2/Michel/resolve/main/check_server.py
- Command line
-
hf download hf://spaces/scomb2/Michel/check_server.py
-
curl -L -o check_server.py https://huggingface.co/spaces/scomb2/Michel/resolve/main/check_server.py
1.38 kB
| """Start the server briefly and check it responds to /health.""" | |
| import json | |
| import os | |
| import subprocess | |
| import sys | |
| import time | |
| import urllib.request | |
| os.chdir(os.path.dirname(__file__)) | |
| env = os.environ.copy() | |
| allow_degraded = env.get("CHECK_SERVER_ALLOW_DEGRADED", "").lower() in { | |
| "1", | |
| "true", | |
| "yes", | |
| "on", | |
| } | |
| if allow_degraded: | |
| env.setdefault("ALLOW_DEGRADED_STARTUP", "1") | |
| p = subprocess.Popen( | |
| [sys.executable, "-m", "uvicorn", "backend.main:app", "--port", "8001"], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| env=env, | |
| ) | |
| # give it up to 20s to start | |
| started = False | |
| for _ in range(20): | |
| time.sleep(1) | |
| try: | |
| with urllib.request.urlopen("http://localhost:8001/health", timeout=2) as r: | |
| body = json.loads(r.read()) | |
| print("Server health:", body) | |
| status = body.get("status") | |
| if status == "healthy": | |
| started = True | |
| break | |
| if allow_degraded and status == "degraded": | |
| started = True | |
| break | |
| except Exception: | |
| pass | |
| if not started: | |
| print("Server did not start in time — stdout so far:") | |
| p.terminate() | |
| out, _ = p.communicate(timeout=5) | |
| print(out.decode(errors="replace")) | |
| sys.exit(1) | |
| p.terminate() | |
| p.wait(timeout=5) | |
| print("Server started and responded correctly.") | |