Complete versioned attachment processing and fault invariants

This commit is contained in:
Artyom
2026-11-06 09:46:43 +03:00
parent e9e1ce576c
commit ac6c55809f
19 changed files with 474 additions and 46 deletions
@@ -519,6 +519,19 @@ class PostgresJobRepository:
await self._enqueue(job)
await self._note(job, "enqueued")
async def reprocess(self, job: Job, operator: str) -> bool:
existing = await self._session.scalar(
select(JobRow.job_id).where(
JobRow.attachment_id == job.attachment_id,
JobRow.pipeline_version == job.pipeline_version,
)
)
if existing is not None:
return False
await self.add(job)
await self._note(job, "reprocess_requested", {"operator": operator})
return True
async def claim(self, job: Job, token: UUID, lease: timedelta) -> Claim | None:
row = (
await self._session.execute(
@@ -686,8 +699,31 @@ class PostgresJobRepository:
)
if attempt is None:
return False
await self._move(
lease.job.attachment_id, ["queued", "processing"], "failed", error_code=code.value
await self._session.execute(
select(AttachmentRow.id)
.where(AttachmentRow.id == lease.job.attachment_id)
.with_for_update()
)
active = (
select(JobRow.job_id)
.where(
JobRow.attachment_id == lease.job.attachment_id,
JobRow.status.in_(["queued", "processing"]),
)
.exists()
)
await self._session.execute(
update(AttachmentRow)
.where(
AttachmentRow.id == lease.job.attachment_id,
AttachmentRow.status.in_(["queued", "processing"]),
~active,
)
.values(
status="failed",
error_code=code.value,
updated_at=func.greatest(func.now(), AttachmentRow.created_at),
)
)
await self._note(
lease.job.retried(attempt), "failed", {"error_code": code.value, "error": error}