#!/usr/bin/env python3 """Worker resource limits (level 4): a 36 MP image is processed normally; the same image under a tight RLIMIT_AS fails permanently (image_too_large, 1 attempt); under a tight time limit the child process is killed (processing_timeout, transient) until retry_exhausted. The worker is recreated with overridden env and restored.""" import time from urllib.parse import quote from stack import dc, inspect, png from client import Client, arguments, check, run BIG = None def big_png(): global BIG if BIG is None: BIG = png(6000, 6000) # 36 MP RGB: ~108 MB decoded, ~110 KB compressed (below the 10 MiB byte limit) return BIG def main(): args = arguments("Worker time/memory limits") c = Client(args.base_url, timeout=30) alice, bob, _eve, chat = c.bootstrap() def upload(): path = "/api/v1/chats/" + chat["id"] + "/attachments?filename=" + quote("big.png") return c.call("POST", path, 202, actor=alice, raw=big_png(), headers={"Content-Type": "image/png"}, schema="Attachment")["json"] def final(att, timeout=120): deadline = time.monotonic() + timeout while time.monotonic() < deadline: a = c.call("GET", "/api/v1/attachments/" + att["id"], actor=bob, schema="Attachment")["json"] if a["status"] in ("ready", "failed"): return a time.sleep(0.5) raise AssertionError("no final state") def worker(**env): dc("up", "-d", "--wait", "--no-build", "--force-recreate", "worker", env={k: str(v) for k, v in env.items()}) print("big image: %d bytes compressed, 6000x6000" % len(big_png())) try: worker() started = time.monotonic() a = final(upload()) check(a["status"] == "ready", "36 MP must be processed with default limits") dims = {v["name"]: (v["width"], v["height"]) for v in a["variants"]} check(dims == {"thumbnail": (256, 256), "crop": (128, 128), "optimized": (1280, 1280)}, "dims %s" % dims) print("PASS default limits (768 MiB, 60 s): ready in %.1fs, %s" % (time.monotonic() - started, dims)) worker(WORKER_MEMORY_LIMIT_MB=160) a = final(upload()) info = inspect(a["id"]) check(a["status"] == "failed" and a["error_code"] == "image_too_large", "memory limit -> %s" % a["error_code"]) check(info["jobs"][0]["attempt"] == 1, "memory limit is permanent, no retries") print("PASS RLIMIT_AS 160 MiB: failed/image_too_large after 1 attempt") worker(JOB_TIMEOUT_SECONDS=0.3, RETRY_BASE_SECONDS=0.5, RETRY_MAX_SECONDS=2) a = final(upload()) info = inspect(a["id"]) check(a["status"] == "failed" and a["error_code"] == "retry_exhausted", "timeout -> %s" % a["error_code"]) errors = [e["detail"].split(";")[0] for e in info["events"] if e["event"] == "retry_scheduled"] check(len(errors) == 4 and all(x == "processing_timeout" for x in errors), "timeouts %s" % errors) print("PASS JOB_TIMEOUT 0.3 s: child killed each attempt, %d retries, then failed/retry_exhausted" % len(errors)) finally: worker() print("PASS: worker limits") if __name__ == "__main__": run(main)