"""Операторский replay failed-задач: failed → queued + запись в outbox. Использование: python -m app.storage.replay_failed [ ...] python -m app.storage.replay_failed --all-failed Защита: - переводит ТОЛЬКО failed; queued/processing/ready не трогает (возвращает "already"); - новая строка outbox с новым job_id — публикация через существующий outbox-процесс; - идемпотентность эффекта по (attachment_id, pipeline_version, variant) не даёт создать дубли вариантов, если повторная обработка уже была выполнена. Логи пишутся только с ID и статусом, без секретов и содержимого файлов. """ import logging import sys import uuid from .db import init_pool, close_pool from .postgres import storage logging.basicConfig( level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s", stream=sys.stdout, ) log = logging.getLogger("replay_failed") def _parse_args(argv): if not argv: print( "usage: python -m app.storage.replay_failed [...] | --all-failed", file=sys.stderr, ) return None if argv == ["--all-failed"]: return "all" ids = [] for raw in argv: try: ids.append(uuid.UUID(raw)) except ValueError: print("invalid attachment_id: %r" % raw, file=sys.stderr) return None return ids def _list_failed_ids() -> list: from .db import connection with connection() as conn, conn.cursor() as cur: cur.execute("SELECT id FROM attachments WHERE status='failed' ORDER BY updated_at") return [r[0] for r in cur.fetchall()] def main(argv) -> int: parsed = _parse_args(argv) if parsed is None: return 2 init_pool() try: if parsed == "all": ids = _list_failed_ids() else: ids = parsed requeued = already = missing = 0 for aid in ids: result = storage.replay_failed_to_queued(aid) if result == "requeued": requeued += 1 log.info("event=replay_requeued attachment_id=%s", aid) elif result == "already": already += 1 log.info("event=replay_skipped_not_failed attachment_id=%s", aid) else: missing += 1 log.info("event=replay_missing attachment_id=%s", aid) log.info( "event=replay_done requested=%d requeued=%d skipped=%d missing=%d", len(ids), requeued, already, missing, ) return 0 if missing == 0 else 1 finally: close_pool() if __name__ == "__main__": sys.exit(main(sys.argv[1:]))