File size: 1,377 Bytes
697288c
 
 
 
 
 
 
 
202d151
 
 
697288c
 
 
 
 
 
 
 
 
 
202d151
 
 
 
697288c
202d151
 
 
 
 
 
 
 
 
 
697288c
 
 
 
 
 
 
202d151
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
"""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.")