Files
mtusi2026lab5/scripts/scripts/restart.py
T

50 lines
1.3 KiB
Python

#!/usr/bin/env python3
import subprocess
import sys
import time
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
BASE_URL = "http://localhost:8080"
def run(cmd):
print(f"[restart] $ {' '.join(cmd)}", flush=True)
result = subprocess.run(cmd, cwd=ROOT)
if result.returncode != 0:
print(
f"[restart] command failed with code {result.returncode}",
file=sys.stderr,
)
sys.exit(result.returncode)
def main():
run(["docker", "compose", "--env-file", ".env", "down"])
run([
"docker", "compose", "--env-file", ".env",
"up", "-d", "--build", "--scale", "api=2",
])
print("[restart] waiting for /health/ready", flush=True)
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
try:
with urllib.request.urlopen(
f"{BASE_URL}/health/ready", timeout=2
) as r:
if r.status == 200:
print("[restart] api is ready", flush=True)
return 0
except Exception:
time.sleep(1)
print(
"[restart] api did not become ready in 90 seconds",
file=sys.stderr,
)
return 1
if __name__ == "__main__":
sys.exit(main())