#!/usr/bin/env python3 """Operator CLI for attachment jobs (run inside an API container: docker compose exec api-1 python scripts/jobs.py ...). failed registry of failed jobs (the "DLQ" of business failures) show job state + audit trail replay --actor NAME explicit failed -> queued; refused (no-op) for ready/queued/processing stats counts by state and outbox backlog Poison messages that are not valid jobs are in the RabbitMQ queue attachments.dlq (see REPORT.md). """ import argparse import asyncio import json import sys sys.path.insert(0, "/app") from starlette.concurrency import run_in_threadpool # noqa: E402 from app import jobs # noqa: E402 from app.cursor import fmt # noqa: E402 from app.db import Database # noqa: E402 from app.s3 import S3Storage # noqa: E402 def out(value) -> None: print(json.dumps(value, ensure_ascii=False, indent=2, default=str)) async def main() -> int: parser = argparse.ArgumentParser() sub = parser.add_subparsers(dest="cmd", required=True) sub.add_parser("failed") sub.add_parser("stats") show = sub.add_parser("show") show.add_argument("attachment_id") rp = sub.add_parser("replay") rp.add_argument("attachment_id") rp.add_argument("--actor", required=True, help="who requests the replay (written to job_audit)") args = parser.parse_args() db = Database() await db.open() try: if args.cmd == "failed": async with db.connection() as conn: out(await jobs.failed_jobs(conn)) elif args.cmd == "stats": async with db.connection() as conn: cur = await conn.execute("SELECT state, count(*) AS n FROM attachment_jobs GROUP BY state ORDER BY state") states = {r["state"]: r["n"] for r in await cur.fetchall()} cur = await conn.execute("SELECT count(*) AS n FROM outbox WHERE published_at IS NULL") backlog = (await cur.fetchone())["n"] out({"jobs": states, "outbox_unpublished": backlog}) elif args.cmd == "show": async with db.connection() as conn: cur = await conn.execute( "SELECT j.id, j.state, j.attempt, j.max_attempts, j.error_code, j.lease_owner, j.lease_until," " a.status AS attachment_status FROM attachment_jobs j JOIN attachments a ON a.id = j.attachment_id" " WHERE j.attachment_id = %s", (args.attachment_id,)) job = await cur.fetchone() cur = await conn.execute( "SELECT action, actor, attempt, detail, created_at FROM job_audit WHERE attachment_id = %s ORDER BY id", (args.attachment_id,)) trail = [{**r, "created_at": fmt(r["created_at"])} for r in await cur.fetchall()] cur = await conn.execute( "SELECT o.id, o.payload->>'attempt' AS attempt, o.available_at, o.published_at, o.publish_attempts" " FROM outbox o JOIN attachment_jobs j ON j.id = o.job_id WHERE j.attachment_id = %s ORDER BY o.id", (args.attachment_id,)) outbox = await cur.fetchall() out({"job": job, "audit": trail, "outbox": outbox}) elif args.cmd == "replay": s3 = S3Storage() await run_in_threadpool(s3.open) async def exists(key): return await run_in_threadpool(s3.exists, key) result = await db.in_transaction( lambda conn: jobs.replay(conn, args.attachment_id, "operator:" + args.actor, exists)) out(result) return 0 if result["result"] == "queued" else 3 finally: await db.close() return 0 if __name__ == "__main__": sys.exit(asyncio.run(main()))