forked from mtusi2026/mtusi2026lab5
solution
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
"""Jobs одновременно хранят намерение публикации очередной попытки."""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.attachment import Attachment
|
||||
from app.models.job import AttachmentJob
|
||||
|
||||
|
||||
async def next_unpublished_job(
|
||||
session: AsyncSession, *, attachment_id: UUID | None = None
|
||||
) -> AttachmentJob | None:
|
||||
statement = (
|
||||
select(AttachmentJob)
|
||||
.join(Attachment, Attachment.id == AttachmentJob.attachment_id)
|
||||
.where(
|
||||
Attachment.status == "queued",
|
||||
AttachmentJob.next_attempt_at <= func.clock_timestamp(),
|
||||
AttachmentJob.published_attempt <= AttachmentJob.attempt,
|
||||
)
|
||||
.order_by(AttachmentJob.next_attempt_at, AttachmentJob.id)
|
||||
.limit(1)
|
||||
.with_for_update(of=AttachmentJob, skip_locked=True)
|
||||
)
|
||||
if attachment_id is not None:
|
||||
statement = statement.where(AttachmentJob.attachment_id == attachment_id)
|
||||
return await session.scalar(statement)
|
||||
|
||||
|
||||
def mark_published(job: AttachmentJob) -> None:
|
||||
job.published_attempt = job.attempt + 1
|
||||
Reference in New Issue
Block a user