"""Операторский replay и реестр неудавшихся заданий: python -m app.replay [list | replay | replay-all] list — реестр failed-вложений: id, error_code, число попыток, возраст, наличие staging; replay — возвращает ТОЛЬКО failed-вложение в queued: новая попытка (attempt сбрасывается на 1), новый job_id, запись в job_events (аудит), задание в outbox. Если у вложения уже есть варианты (ready/успешный эффект), повторного исполнения не будет: вложение не в статусе failed -> replay отклоняется. Эффекты защищены ключом (attachment_id, pipeline_version, name): даже повторное выполнение не создаёт второй набор вариантов. """ import argparse import asyncio import json import sys import uuid import asyncpg from . import config async def list_failed(conn): rows = await conn.fetch( "SELECT id, error_code, attempt, staging_key IS NOT NULL AS has_staging, updated_at FROM attachments " "WHERE status = 'failed' ORDER BY updated_at DESC LIMIT 200") return [{"attachment_id": str(r["id"]), "error_code": r["error_code"], "attempt": r["attempt"], "staging_present": r["has_staging"], "failed_at": r["updated_at"].isoformat()} for r in rows] async def replay(conn, att_id): async with conn.transaction(): row = await conn.fetchrow("SELECT id, status, pipeline_version, staging_key FROM attachments WHERE id = $1 FOR UPDATE", att_id) if row is None: return {"attachment_id": str(att_id), "result": "not_found"} if row["status"] != "failed": return {"attachment_id": str(att_id), "result": "skipped", "reason": "status=%s (replay только для failed)" % row["status"]} if not row["staging_key"]: return {"attachment_id": str(att_id), "result": "skipped", "reason": "staging удалён по retention, исходник недоступен"} job_id = uuid.uuid4() await conn.execute("UPDATE attachments SET status = 'queued', error_code = NULL, attempt = 0, job_id = $2, lease_owner = NULL, " "lease_expires_at = NULL, updated_at = now() WHERE id = $1", att_id, job_id) await conn.execute("INSERT INTO outbox (job_id, attachment_id, pipeline_version, attempt) VALUES ($1, $2, $3, 1)", job_id, att_id, row["pipeline_version"]) await conn.execute("INSERT INTO job_events (attachment_id, job_id, event, attempt, detail) VALUES ($1, $2, 'operator_replay', 1, 'failed -> queued')", att_id, job_id) return {"attachment_id": str(att_id), "result": "requeued", "job_id": str(job_id)} async def main_async(args): conn = await asyncpg.connect(config.DATABASE_URL, timeout=5) try: if args.command == "list": return await list_failed(conn) if args.command == "replay": return await replay(conn, uuid.UUID(args.attachment_id)) return [await replay(conn, uuid.UUID(f["attachment_id"])) for f in await list_failed(conn)] finally: await conn.close() def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) sub = ap.add_subparsers(dest="command", required=True) sub.add_parser("list") r = sub.add_parser("replay") r.add_argument("attachment_id") sub.add_parser("replay-all") print(json.dumps(asyncio.run(main_async(ap.parse_args())), ensure_ascii=False, indent=2)) if __name__ == "__main__": sys.exit(main())