solution
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Постоянная ошибка → failed + DLQ; явный replay → новый job и ready."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
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 now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
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)
|
||||
report = jc.wait_job(api, alice, attachment["id"], "queued", timeout=30)
|
||||
original_job = report["jobs"][0]["id"]
|
||||
jc.admin_json("fault", "--kind", "permanent_once", "--count", "1")
|
||||
jc.scale("worker", 1)
|
||||
jc.wait_service("worker", 1)
|
||||
failed = jc.wait_attachment(api, alice, attachment["id"], "failed", timeout=120)
|
||||
failed_report = jc.admin_json("show", "--attachment", attachment["id"])
|
||||
time.sleep(2.0)
|
||||
queues = jc.queue_stats()
|
||||
replay = jc.admin_json(
|
||||
"replay",
|
||||
"--job",
|
||||
failed_report["jobs"][0]["id"],
|
||||
"--operator",
|
||||
"student",
|
||||
"--reason",
|
||||
"retry after transient storage fault was fixed",
|
||||
)
|
||||
# Replay может завершиться быстрее, чем клиент успевает увидеть queued;
|
||||
# поэтому ждём терминальное ready, а не промежуточное состояние.
|
||||
ready = jc.wait_attachment(api, bob, attachment["id"], "ready", timeout=120)
|
||||
final = jc.admin_json("show", "--attachment", attachment["id"])
|
||||
ready_job_id = replay["replay_job_id"]
|
||||
refused = subprocess.run(
|
||||
[
|
||||
"docker",
|
||||
"exec",
|
||||
jc.api_container(),
|
||||
"python",
|
||||
"-m",
|
||||
"messenger.admin",
|
||||
"replay",
|
||||
"--job",
|
||||
ready_job_id,
|
||||
"--operator",
|
||||
"student",
|
||||
"--reason",
|
||||
"must be refused",
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
)
|
||||
evidence = {
|
||||
"started_at": None,
|
||||
"scenario": "permanent_failure_dlq_and_replay",
|
||||
"attachment_id": attachment["id"],
|
||||
"original_job_id": original_job,
|
||||
"failed_attachment_status": failed["status"],
|
||||
"failed_error_code": failed["error_code"],
|
||||
"failed_job": failed_report["jobs"][0],
|
||||
"queues_after_failure": queues,
|
||||
"replay_job_id": replay["replay_job_id"],
|
||||
"replay_job": next(
|
||||
(job for job in final["jobs"] if job["id"] == replay["replay_job_id"]), None
|
||||
),
|
||||
"status_after_replay": ready["status"],
|
||||
"final_variants": [item["name"] for item in final["attachment"]["variants"]],
|
||||
"replay_of_ready_attempt": {
|
||||
"return_code": refused.returncode,
|
||||
"stderr": (refused.stderr or "").strip(),
|
||||
"stdout": (refused.stdout or "").strip(),
|
||||
},
|
||||
"passed": (
|
||||
failed["status"] == "failed"
|
||||
and failed["error_code"] == "invalid_image"
|
||||
and queues.get("attachment.dlq", {}).get("messages", 0) >= 1
|
||||
and ready["status"] == "ready"
|
||||
and len(final["attachment"]["variants"]) == 3
|
||||
and refused.returncode != 0
|
||||
),
|
||||
"note": (
|
||||
"The permanent fault produced failed+error_code and a DLQ message. "
|
||||
"Explicit replay created a new job_id linked by replay_of; the effect "
|
||||
"key stayed the same. Replaying an already ready job was refused."
|
||||
),
|
||||
}
|
||||
output = ROOT / "evidence" / "replay-dlq.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())
|
||||
Reference in New Issue
Block a user