forked from mtusi2026/mtusi2026lab5
105 lines
3.9 KiB
Python
105 lines
3.9 KiB
Python
import argparse
|
|
import json
|
|
import uuid
|
|
from .config import Settings
|
|
from .repository import Repository
|
|
|
|
|
|
def enqueue(db, attachment, version, settings):
|
|
job_id = uuid.uuid4()
|
|
row = db.execute(
|
|
"""INSERT INTO processing_jobs(job_id,attachment_id,pipeline_version,attempt_limit)
|
|
VALUES (%s,%s,%s,%s) RETURNING created_at""",
|
|
(job_id, attachment, version, settings.max_attempts),
|
|
).fetchone()
|
|
payload = {
|
|
"schema_version": 1,
|
|
"job_id": str(job_id),
|
|
"type": "attachment.process.v1",
|
|
"attachment_id": str(attachment),
|
|
"pipeline_version": version,
|
|
"attempt": 1,
|
|
"created_at": row["created_at"].isoformat().replace("+00:00", "Z"),
|
|
}
|
|
db.execute(
|
|
"INSERT INTO processing_outbox(job_id,payload) VALUES (%s,%s::jsonb)",
|
|
(job_id, json.dumps(payload)),
|
|
)
|
|
return {"job_id": job_id, "attempt": 0}
|
|
|
|
|
|
def upgrade(repository, attachment, version):
|
|
with repository.connection() as db:
|
|
db.execute("SELECT pg_advisory_xact_lock_shared(741204)")
|
|
current = db.execute(
|
|
"SELECT * FROM attachments WHERE id=%s FOR UPDATE", (attachment,)
|
|
).fetchone()
|
|
if not current or version <= current["pipeline_version"]:
|
|
raise ValueError("version_must_increase")
|
|
db.execute(
|
|
"UPDATE processing_jobs SET status='SUPERSEDED',lease_until=NULL WHERE attachment_id=%s AND status IN ('QUEUED','PROCESSING','FAILED')",
|
|
(attachment,),
|
|
)
|
|
db.execute(
|
|
"UPDATE attachments SET pipeline_version=%s,status='QUEUED',error_code=NULL,updated_at=now() WHERE id=%s",
|
|
(version, attachment),
|
|
)
|
|
job = enqueue(db, attachment, version, repository.settings)
|
|
repository.event(db, job, "operator_upgrade")
|
|
return {"event": "upgraded", "version": version}
|
|
|
|
|
|
def replay(repository, attachment):
|
|
with repository.connection() as db:
|
|
db.execute("SELECT pg_advisory_xact_lock_shared(741204)")
|
|
current = db.execute(
|
|
"SELECT * FROM attachments WHERE id=%s FOR UPDATE", (attachment,)
|
|
).fetchone()
|
|
if not current:
|
|
raise ValueError("attachment_not_found")
|
|
job = db.execute(
|
|
"SELECT * FROM processing_jobs WHERE attachment_id=%s AND pipeline_version=%s FOR UPDATE",
|
|
(attachment, current["pipeline_version"]),
|
|
).fetchone()
|
|
if job["status"] == "READY":
|
|
repository.event(db, job, "operator_replay_noop")
|
|
return {"event": "already_ready", "replayed": False}
|
|
if job["error_code"] == "staging_expired":
|
|
raise ValueError("staging_expired")
|
|
if job["status"] != "FAILED":
|
|
raise ValueError("only_failed_can_be_replayed")
|
|
reset(db, job, repository.settings.max_attempts)
|
|
repository.event(db, job, "operator_replay")
|
|
return {"event": "replayed", "replayed": True}
|
|
|
|
|
|
def reset(db, job, budget):
|
|
db.execute(
|
|
"""UPDATE processing_jobs SET status='QUEUED',error_code=NULL,lease_token=NULL,
|
|
lease_until=NULL,completed_at=NULL,attempt_limit=attempt+%s,next_at=now(),next_publish_at=now() WHERE job_id=%s""",
|
|
(budget, job["job_id"]),
|
|
)
|
|
db.execute(
|
|
"UPDATE attachments SET status='QUEUED',error_code=NULL,updated_at=now() WHERE id=%s",
|
|
(job["attachment_id"],),
|
|
)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("action", choices=["upgrade", "replay"])
|
|
parser.add_argument("attachment", type=uuid.UUID)
|
|
parser.add_argument("--version", type=int, default=2)
|
|
args = parser.parse_args()
|
|
repository = Repository(Settings())
|
|
result = (
|
|
upgrade(repository, args.attachment, args.version)
|
|
if args.action == "upgrade"
|
|
else replay(repository, args.attachment)
|
|
)
|
|
print(json.dumps(result), flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|