Files

19 lines
1.2 KiB
Python

"""Explicitly replay one terminal failed attachment job, never a ready job."""
import sys, uuid
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from app.migrate import setting
import psycopg
if len(sys.argv)!=2:
raise SystemExit('usage: python scripts/replay_failed.py ATTACHMENT_UUID')
aid=str(uuid.UUID(sys.argv[1]))
with psycopg.connect(setting('DATABASE_URL')) as c:
row=c.execute("SELECT j.job_id,j.state,a.status FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.id=%s FOR UPDATE OF j,a",(aid,)).fetchone()
if not row or row[1]!='failed' or row[2]!='failed':
raise SystemExit('replay refused: attachment must be terminal failed')
c.execute("UPDATE attachment_jobs SET state='pending',attempt=0,available_at=clock_timestamp(),lease_until=NULL,last_error=NULL WHERE job_id=%s",(row[0],))
c.execute("UPDATE attachments SET status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=%s",(aid,))
c.execute("INSERT INTO attachment_job_audit(job_id,event,detail) VALUES (%s,'operator_replay','explicit replay command')",(row[0],))
print('failed job queued for explicit replay')