This commit is contained in:
2026-11-22 17:59:11 +03:00
parent 7e1780be67
commit 813fc03da5
18 changed files with 1859 additions and 265 deletions
+35
View File
@@ -1,13 +1,19 @@
"""Job payload for the broker. Bytes and credentials never go into the message."""
import json
import random
import re
import uuid
from datetime import datetime, timezone
from typing import Optional
JOB_TYPE = 'attachment.process.v1'
PIPELINE_VERSION = 1
QUEUE = 'attachment.jobs'
DLQ = 'attachment.jobs.dlq'
# Same arguments on every declare, otherwise RabbitMQ rejects the second one.
QUEUE_ARGUMENTS = {'x-queue-type': 'classic'}
DLQ_ARGUMENTS = {'x-queue-type': 'classic'}
REQUIRED_VARIANTS = ('thumbnail', 'crop', 'optimized')
_UUID = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$')
@@ -36,6 +42,35 @@ def variant_key(attachment_id: str, name: str) -> str:
return 'variants/' + attachment_id + '/' + name
def lock_key(attachment_id: uuid.UUID) -> int:
"""Stable bigint for a session advisory lock. One attachment, one runner."""
return attachment_id.int & ((1 << 63) - 1)
def format_timestamp(moment: datetime) -> str:
if moment.tzinfo is None:
moment = moment.replace(tzinfo=timezone.utc)
text = moment.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z')
if not text.endswith('Z') or len(text) > 40:
raise ValueError('timestamp does not fit the job contract')
return text
def backoff_seconds(attempt: int, base: float, cap: float, rng: Optional[random.Random] = None) -> float:
"""Full jitter. The ceiling is min(cap, base * 2^(attempt-1)); the draw is uniform below it."""
step = attempt - 1
if step < 0:
step = 0
if step > 20:
step = 20
ceiling = min(cap, base * float(2 ** step))
if ceiling <= 0:
return 0.0
if rng is None:
return random.uniform(0, ceiling)
return rng.uniform(0, ceiling)
def dump_job(job_id: str, attachment_id: str, attempt: int, created_at: str) -> bytes:
payload = {
'schema_version': 1,