Files
mtusi2026lab2/src/messenger/attachments/domain/jobs.py
T
ArtyomandClaude Opus 5.5 5ae3d77152 Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 03:51:58 +03:00

96 lines
2.2 KiB
Python

from dataclasses import dataclass, replace
from datetime import UTC, datetime, timedelta
from enum import StrEnum
from typing import Any
from uuid import UUID
JOB_TYPE = "attachment.process.v1"
SCHEMA_VERSION = 1
class FailureCode(StrEnum):
INVALID_IMAGE = "invalid_image"
PROCESSING_FAILED = "processing_failed"
RETRY_EXHAUSTED = "retry_exhausted"
class ReplayResult(StrEnum):
REPLAYED = "replayed"
NOOP = "noop"
EXPIRED = "expired"
NOT_FOUND = "not_found"
class Outcome(StrEnum):
DONE = "done"
DUPLICATE = "duplicate_noop"
FENCED = "fenced"
SUPERSEDED = "superseded"
RESCHEDULED = "retry_scheduled"
LEASE_EXPIRED = "lease_expired"
FAILED = "failed"
@dataclass(frozen=True, slots=True)
class Job:
id: UUID
attachment_id: UUID
pipeline_version: int
attempt: int
created_at: datetime
def payload(self) -> dict[str, Any]:
stamp = self.created_at.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
return {
"schema_version": SCHEMA_VERSION,
"job_id": str(self.id),
"type": JOB_TYPE,
"attachment_id": str(self.attachment_id),
"pipeline_version": self.pipeline_version,
"attempt": self.attempt,
"created_at": stamp,
}
def retried(self, attempt: int) -> Job:
return replace(self, attempt=attempt)
@dataclass(frozen=True, slots=True)
class Lease:
job: Job
token: UUID
@dataclass(frozen=True, slots=True)
class Claim:
lease: Lease
source_key: str
staged: bool
@dataclass(frozen=True, slots=True)
class FailedJob:
job_id: UUID
attachment_id: UUID
pipeline_version: int
attempt: int
error_code: str | None
last_error: str | None
failed_at: datetime
@dataclass(frozen=True, slots=True)
class RetryPolicy:
max_attempts: int
base_seconds: float
cap_seconds: float
def exhausted(self, attempt: int) -> bool:
return attempt >= self.max_attempts
def delay(self, attempt: int, spread: float) -> timedelta | None:
if self.exhausted(attempt):
return None
ceiling = min(self.cap_seconds, self.base_seconds * 2 ** (attempt - 1))
return timedelta(seconds=ceiling / 2 * (1 + spread))