forked from mtusi2026/mtusi2026lab5
Реализовать lab5 на оценку 4: async attachments, outbox, worker и variants.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Operator replay of failed attachment jobs (lab5 grade 4).
|
||||
|
||||
Usage (inside API/worker network or with secrets mounted):
|
||||
python scripts/replay_failed.py --dead-letter-id N
|
||||
python scripts/replay_failed.py --attachment-id UUID
|
||||
python -m messenger.replay --list
|
||||
|
||||
Safe against ready results: already-ready attachments are skipped, not re-executed.
|
||||
Staging for the attachment is preserved; janitor will not delete it while queued.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "src"))
|
||||
|
||||
from messenger import db # noqa: E402
|
||||
from messenger.outbox import OutboxPublisher # noqa: E402
|
||||
from messenger.store import Store # noqa: E402
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--dead-letter-id", type=int, default=None)
|
||||
parser.add_argument("--attachment-id", default=None)
|
||||
parser.add_argument("--list", action="store_true", help="list open dead letters")
|
||||
parser.add_argument("--note", default="operator replay")
|
||||
args = parser.parse_args()
|
||||
db.init_pool()
|
||||
store = Store()
|
||||
try:
|
||||
if args.list:
|
||||
with db.get_pool().connection() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, job_id, attachment_id, error_code, attempts, failed_at
|
||||
FROM dead_letter_jobs
|
||||
WHERE replayed_at IS NULL
|
||||
ORDER BY failed_at
|
||||
LIMIT 50
|
||||
"""
|
||||
).fetchall()
|
||||
for r in rows:
|
||||
print(
|
||||
f"id={r['id']} attachment={r['attachment_id']} "
|
||||
f"error={r['error_code']} attempts={r['attempts']}"
|
||||
)
|
||||
print(f"open_dead_letters={len(rows)}")
|
||||
return 0
|
||||
dead_id = args.dead_letter_id
|
||||
if dead_id is None and args.attachment_id:
|
||||
with db.get_pool().connection() as conn:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT id FROM dead_letter_jobs
|
||||
WHERE attachment_id = %s AND replayed_at IS NULL
|
||||
ORDER BY failed_at DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
(args.attachment_id,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
print("no open dead letter for attachment", file=sys.stderr)
|
||||
return 2
|
||||
dead_id = int(row["id"])
|
||||
if dead_id is None:
|
||||
parser.error("need --dead-letter-id or --attachment-id or --list")
|
||||
result = store.replay_dead_letter(dead_id, note=args.note)
|
||||
if result is None:
|
||||
print("dead letter not found", file=sys.stderr)
|
||||
return 2
|
||||
OutboxPublisher(store).flush_once()
|
||||
print(f"replay_result={result} dead_letter_id={dead_id}")
|
||||
return 0
|
||||
finally:
|
||||
db.close_pool()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user