лаба на 4
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import text
|
||||
|
||||
from src.database import SessionFactory, engine
|
||||
|
||||
|
||||
async def replay(job_id: UUID):
|
||||
async with SessionFactory() as session:
|
||||
async with session.begin():
|
||||
result = await session.execute(
|
||||
text("""
|
||||
SELECT j.id, j.attachment_id, j.status,
|
||||
j.generation, a.status AS attachment_status
|
||||
FROM attachment_jobs j
|
||||
JOIN attachments a ON a.id = j.attachment_id
|
||||
WHERE j.id = :job_id
|
||||
FOR UPDATE OF j, a
|
||||
"""),
|
||||
{"job_id": job_id},
|
||||
)
|
||||
|
||||
row = result.one_or_none()
|
||||
|
||||
if row is None:
|
||||
print("REJECTED: job not found")
|
||||
return
|
||||
|
||||
if row.status != "failed" or row.attachment_status != "failed":
|
||||
print("REJECTED: job and attachment must be failed")
|
||||
return
|
||||
|
||||
await session.execute(
|
||||
text("""
|
||||
UPDATE attachment_jobs
|
||||
SET status = 'queued',
|
||||
generation = generation + 1,
|
||||
attempt = 1,
|
||||
published_at = NULL,
|
||||
started_at = NULL,
|
||||
next_attempt_at = NULL,
|
||||
last_error_code = NULL,
|
||||
finished_at = NULL,
|
||||
failed_published_at = NULL
|
||||
WHERE id = :job_id
|
||||
"""),
|
||||
{"job_id": job_id},
|
||||
)
|
||||
|
||||
await session.execute(
|
||||
text("""
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
error_code = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = :attachment_id
|
||||
"""),
|
||||
{"attachment_id": row.attachment_id},
|
||||
)
|
||||
|
||||
print(
|
||||
f"REPLAY ACCEPTED: job={job_id}, "
|
||||
f"generation={row.generation + 1}"
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("job_id", type=UUID)
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
await replay(args.job_id)
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user