#!/usr/bin/env python3 """Restart API and all used stores, preserving volumes. Called by tests/resilience.py via --action-script. Must NOT remove volumes or re-seed the database. """ 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"]) 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())