"""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.")