Prove versioned pipeline recovery and audited replay (U4)
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""Explicit, audited replay of one failed current-version job."""
|
||||
|
||||
import argparse
|
||||
from uuid import UUID
|
||||
|
||||
from .db import pool
|
||||
|
||||
|
||||
def replay_failed(conn, job_id: UUID, operator: str, reason: str) -> int:
|
||||
if not 1 <= len(operator) <= 80 or not 1 <= len(reason) <= 200:
|
||||
raise ValueError("operator and reason are required")
|
||||
with conn.transaction():
|
||||
job = conn.execute(
|
||||
"SELECT j.*,a.status AS attachment_status,"
|
||||
"a.pipeline_version AS current_pipeline_version "
|
||||
"FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id "
|
||||
"WHERE j.id=%s FOR UPDATE OF j,a",
|
||||
(job_id,),
|
||||
).fetchone()
|
||||
if (job is None or job["status"] != "failed" or
|
||||
job["pipeline_version"] != job["current_pipeline_version"] or
|
||||
job["attachment_status"] != "failed"):
|
||||
raise ValueError("only a failed current-version job can be replayed")
|
||||
attempt = job["attempt"] + 1
|
||||
conn.execute(
|
||||
"UPDATE attachment_jobs SET status='queued',attempt=%s,replay_start_attempt=%s,"
|
||||
"next_attempt_at=clock_timestamp(),error_code=NULL,updated_at=clock_timestamp() "
|
||||
"WHERE id=%s",
|
||||
(attempt, attempt, job_id),
|
||||
)
|
||||
conn.execute(
|
||||
"UPDATE attachments SET status='queued',error_code=NULL,updated_at=clock_timestamp() "
|
||||
"WHERE id=%s AND pipeline_version=%s",
|
||||
(job["attachment_id"], job["pipeline_version"]),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO attachment_outbox (job_id,attempt) VALUES (%s,%s)",
|
||||
(job_id, attempt),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO attachment_job_replays "
|
||||
"(job_id,previous_attempt,new_attempt,previous_error_code,operator_name,reason) "
|
||||
"VALUES (%s,%s,%s,%s,%s,%s)",
|
||||
(job_id, job["attempt"], attempt, job["error_code"], operator, reason),
|
||||
)
|
||||
return attempt
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("job_id", type=UUID)
|
||||
parser.add_argument("--operator", required=True)
|
||||
parser.add_argument("--reason", required=True)
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
with pool().connection() as conn:
|
||||
attempt = replay_failed(conn, args.job_id, args.operator, args.reason)
|
||||
except ValueError as exc:
|
||||
parser.error(str(exc))
|
||||
print(f"Queued job {args.job_id} attempt {attempt}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user