Files
mtusi2026lab7/scripts/demo_worker_restart.py
T
2026-11-28 12:29:50 +01:00

56 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Level-3 demonstration: accepted work survives a stopped worker.
Stops only the worker, uploads (expects 202 queued and 409 on content), then starts
the worker again and waits for ready + a valid 256x160 thumbnail.
"""
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
def compose(*args):
script = ROOT / "scripts" / ("stop-worker.cmd" if sys.platform == "win32" else "stop-worker.sh")
subprocess.run([str(script), *args], cwd=str(ROOT), check=True,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def main():
c = Client("http://localhost:8080")
alice, bob, _eve, chat = c.bootstrap()
compose("stop") # worker down, broker/API/S3 intact
attachment, original = c.attachment(alice, chat)
check(attachment["status"] == "queued", "upload must be 202 queued")
c.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", 409, actor=bob)
print("ACCEPTED:", attachment["id"], "status=queued; content=409 while worker is stopped")
compose("start")
deadline = time.monotonic() + 90
while True:
ready = c.call("GET", "/api/v1/attachments/" + attachment["id"], 200,
actor=bob, schema="Attachment")["json"]
if ready["status"] == "ready":
break
check(ready["status"] != "failed", "job reached failed")
check(time.monotonic() < deadline, "job did not reach ready within 90 s")
time.sleep(0.3)
thumb = [v for v in ready["variants"] if v["name"] == "thumbnail"]
check(len(thumb) == 1 and (thumb[0]["width"], thumb[0]["height"]) == (256, 160),
"thumbnail missing or wrong size")
check(len(c.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content",
200, actor=bob)["bytes"]) == len(original), "original changed")
c.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content?variant=thumbnail",
200, actor=bob)
print("RECOVERED:", attachment["id"], "status=ready, thumbnail=256x160, original intact")
if __name__ == "__main__":
main()