60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
"""Операторские выборки и журнал replay; транзакция принадлежит domain."""
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.dialects.postgresql import insert
|
|
|
|
from app.models.attachment import Attachment
|
|
from app.models.job import AttachmentJob
|
|
from app.models.job_replay import JobReplay
|
|
|
|
|
|
async def failed_jobs(session, limit: int):
|
|
result = await session.execute(
|
|
select(AttachmentJob, Attachment)
|
|
.join(Attachment, Attachment.id == AttachmentJob.attachment_id)
|
|
.where(Attachment.status == "failed")
|
|
.order_by(AttachmentJob.finished_at.desc(), AttachmentJob.id)
|
|
.limit(limit)
|
|
)
|
|
return result.all()
|
|
|
|
|
|
async def replay_record(session, request_id):
|
|
return await session.get(JobReplay, request_id)
|
|
|
|
|
|
async def insert_replay(session, **values) -> bool:
|
|
result = await session.scalar(
|
|
insert(JobReplay)
|
|
.values(**values)
|
|
.on_conflict_do_nothing(index_elements=[JobReplay.id])
|
|
.returning(JobReplay.id)
|
|
)
|
|
return result is not None
|
|
|
|
|
|
async def replay_history(session, job_id):
|
|
return (
|
|
await session.scalars(
|
|
select(JobReplay)
|
|
.where(JobReplay.job_id == job_id)
|
|
.order_by(JobReplay.created_at)
|
|
.limit(100)
|
|
)
|
|
).all()
|
|
|
|
|
|
def reset_failed(job, attachment, now, attempt_limit):
|
|
job.cycle_start_attempt = job.attempt
|
|
job.attempt_limit = attempt_limit
|
|
job.published_attempt = job.attempt
|
|
job.next_attempt_at = now
|
|
job.lease_token = None
|
|
job.lease_until = None
|
|
job.finished_at = None
|
|
job.last_error_code = None
|
|
job.last_error_permanent = None
|
|
attachment.status = "queued"
|
|
attachment.error_code = None
|
|
attachment.updated_at = now
|