Files
mtusi2026lab3/scripts/rolling_update.py
T

45 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Restart API replicas one at a time while the public load continues."""
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def compose(*args):
subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True)
def wait_ready(timeout=60):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
result = subprocess.run(
["python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready', timeout=2)"],
cwd=ROOT,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if result.returncode == 0:
return
time.sleep(0.5)
raise RuntimeError("LB never recovered readiness")
def main():
for service in ("api-1", "api-2"):
compose("stop", "--timeout", "5", service)
time.sleep(2)
compose("start", service)
wait_ready()
print(f"rolling restart recovered: {service}")
if __name__ == "__main__":
try:
main()
except subprocess.CalledProcessError as exc:
print(f"rolling update failed: {exc}", file=sys.stderr)
sys.exit(1)