Files
mtusi2026lab5/src/replay.py
T

47 lines
1.5 KiB
Python

"""Explicit operator replay of failed attachment jobs (lab5 grade-4).
docker compose exec -T api python -m src.replay # list failed
docker compose exec -T api python -m src.replay <job_id> # replay one
Replaying resets the job to ``pending`` and the attachment to ``queued``; the
outbox publisher republishes it. The idempotent effect key is
``(attachment_id, pipeline_version, variant)``, so replaying an already-succeeded
job can never duplicate variants.
"""
from __future__ import annotations
import argparse
import json
from . import repositories as repo
from .db import pool
def main() -> None:
parser = argparse.ArgumentParser(description="List or replay failed attachment jobs")
parser.add_argument("job_id", nargs="?", help="specific job id to replay")
args = parser.parse_args()
with pool().connection() as conn:
if args.job_id is None:
failed = repo.list_failed_jobs(conn)
print(json.dumps(
[
{
"job_id": str(row["job_id"]),
"attachment_id": str(row["attachment_id"]),
"attempt": int(row["attempt"]),
"error_code": row["last_error"],
}
for row in failed
],
ensure_ascii=False,
))
return
replayed = repo.replay_failed_jobs(conn, args.job_id)
print(json.dumps({"replayed": replayed}))
if __name__ == "__main__":
main()