forked from mtusi2026/mtusi2026lab2
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Recreate API with SESSION_TTL_SECONDS=2, test expiry, restore TTL."""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def compose(args, extra_env=None):
|
|
env = os.environ.copy()
|
|
if extra_env:
|
|
env.update(extra_env)
|
|
cmd = ["docker", "compose"]
|
|
if (ROOT / ".env").is_file():
|
|
cmd.extend(["--env-file", str(ROOT / ".env")])
|
|
cmd.extend(args)
|
|
proc = subprocess.run(cmd, cwd=ROOT, env=env)
|
|
if proc.returncode != 0:
|
|
raise SystemExit(proc.returncode)
|
|
|
|
|
|
def wait_ready():
|
|
import urllib.request
|
|
|
|
deadline = time.monotonic() + 90
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
urllib.request.urlopen("http://127.0.0.1:8080/health/ready", timeout=2)
|
|
return
|
|
except Exception:
|
|
time.sleep(0.5)
|
|
raise SystemExit("API did not become ready")
|
|
|
|
|
|
def main():
|
|
compose(["up", "-d", "--no-deps", "--force-recreate", "api"], {"SESSION_TTL_SECONDS": "2"})
|
|
wait_ready()
|
|
env = os.environ.copy()
|
|
env["SESSION_TTL_SECONDS"] = "2"
|
|
env["RUN_EXPIRY"] = "1"
|
|
proc = subprocess.run([sys.executable, str(ROOT / "student-tests" / "expiry.py")], cwd=ROOT, env=env)
|
|
compose(["up", "-d", "--no-deps", "--force-recreate", "api"], {"SESSION_TTL_SECONDS": "3600"})
|
|
wait_ready()
|
|
raise SystemExit(proc.returncode)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|