#!/usr/bin/env python3 """Backoff/retry и исчерпание попыток: transient → retry, затем failed + DLQ, replay.""" from __future__ import annotations import json import sys import time from pathlib import Path HERE = Path(__file__).resolve().parent ROOT = HERE.parent for candidate in (str(HERE), str(ROOT / "tests")): if candidate not in sys.path: sys.path.insert(0, candidate) import job_control as jc # noqa: E402 PNG = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes() def main() -> int: api = jc.client(timeout=20) api.call("GET", "/health/ready", schema="Health") alice, bob, eve, chat = api.bootstrap() jc.scale("worker", 0) jc.wait_service("worker", 0) attachment = jc.upload(api, alice, chat, PNG) job = jc.wait_job(api, alice, attachment["id"], "queued", timeout=30)["jobs"][0] # Пять временных отказов: ровно столько попыток разрешено JOB_MAX_ATTEMPTS. jc.admin_json("fault", "--kind", "transient_always", "--count", "5") jc.scale("worker", 1) jc.wait_service("worker", 1) failed = jc.wait_attachment(api, alice, attachment["id"], "failed", timeout=180) report = jc.admin_json("show", "--attachment", attachment["id"]) time.sleep(1.0) queues = jc.queue_stats() replay = jc.admin_json( "replay", "--job", job["id"], "--operator", "student", "--reason", "bound retries demonstrated; transient fault cleared", ) ready = jc.wait_attachment(api, bob, attachment["id"], "ready", timeout=120) final = jc.admin_json("show", "--attachment", attachment["id"]) retries = [ event for event in report["events"] if event["to_state"] == "queued" and event["detail"] and "retry_in=" in (event["detail"] or "") ] evidence = { "scenario": "bounded_retry_then_failed_and_replay", "attachment_id": attachment["id"], "job_id": job["id"], "max_attempts": report["jobs"][0]["max_attempts"], "attempts": report["jobs"][0]["attempts"], "failed_status": failed["status"], "failed_error_code": failed["error_code"], "job_state": report["jobs"][0]["state"], "retry_events": retries, "retry_delays_seconds": [ float(event["detail"].split("retry_in=")[1].rstrip("s")) for event in retries ], "queues_after_failure": queues, "replay_job_id": replay["replay_job_id"], "status_after_replay": ready["status"], "final_variants": [item["name"] for item in final["attachment"]["variants"]], "passed": ( failed["status"] == "failed" and failed["error_code"] == "retry_exhausted" and report["jobs"][0]["attempts"] == 5 and len(retries) == 4 and queues.get("attachment.dlq", {}).get("messages", 0) >= 1 and ready["status"] == "ready" and len(final["attachment"]["variants"]) == 3 ), "note": ( "Transient failures produced four backoff retries with jitter, then the " "fifth attempt exhausted the configured limit and produced failed+DLQ. " "An explicit replay after clearing the fault completed the same effect key." ), } output = ROOT / "evidence" / "retry.json" output.write_text(json.dumps(evidence, ensure_ascii=False, indent=2, sort_keys=True), encoding="utf-8") print(json.dumps(evidence, ensure_ascii=False, indent=2, sort_keys=True)) return 0 if evidence["passed"] else 1 if __name__ == "__main__": raise SystemExit(main())