"""Shared constants and helpers for the API, the worker and the dispatcher (lab 5).""" from __future__ import annotations import json import os import random import uuid from datetime import datetime, timezone import boto3 from botocore.config import Config as BotoConfig from opentelemetry import trace from opentelemetry.propagate import inject KAFKA_BOOTSTRAP = os.getenv("KAFKA_BOOTSTRAP_SERVERS", "kafka:9092") JOBS_TOPIC = os.getenv("JOBS_TOPIC", "attachment-jobs") PIPELINE_VERSION = 1 JOB_TYPE = "attachment.process.v1" JOB_MAX_ATTEMPTS = int(os.getenv("JOB_MAX_ATTEMPTS", "5")) JOB_TIMEOUT_SECONDS = int(os.getenv("JOB_TIMEOUT_SECONDS", "60")) LEASE_GRACE_SECONDS = int(os.getenv("LEASE_GRACE_SECONDS", "15")) LEASE_SECONDS = JOB_TIMEOUT_SECONDS + LEASE_GRACE_SECONDS # lease > hard job timeout: a live worker never loses it BACKOFF_BASE_SECONDS = float(os.getenv("JOB_BACKOFF_BASE_SECONDS", "2")) BACKOFF_CAP_SECONDS = float(os.getenv("JOB_BACKOFF_CAP_SECONDS", "60")) S3_ENDPOINT = os.getenv("S3_ENDPOINT", "http://s3:9000") S3_REGION = os.getenv("S3_REGION", "us-east-1") S3_BUCKET = os.getenv("S3_BUCKET", "messenger-attachments") STAGING_PREFIX = "staging/" FINAL_PREFIX = "attachments/" VARIANT_ORDER = ("thumbnail", "crop", "optimized") def final_key(attachment_id, pipeline_version: int, name: str) -> str: """Deterministic key: the effect key (attachment_id, pipeline_version, variant) maps 1:1 to an S3 object.""" return f"{FINAL_PREFIX}{attachment_id}/v{pipeline_version}/{name}" def make_s3(): return boto3.client( "s3", endpoint_url=S3_ENDPOINT, region_name=S3_REGION, aws_access_key_id=os.getenv("S3_ACCESS_KEY_ID", ""), aws_secret_access_key=os.getenv("S3_SECRET_ACCESS_KEY", ""), config=BotoConfig(signature_version="s3v4", s3={"addressing_style": "path"}, connect_timeout=2, read_timeout=20, retries={"max_attempts": 2, "mode": "standard"}, request_checksum_calculation="when_required", response_checksum_validation="when_required")) def backoff_seconds(attempt: int) -> float: """Exponential backoff with 'equal jitter': delay in [d/2, d], d = min(cap, base * 2^(attempt-1)).""" d = min(BACKOFF_CAP_SECONDS, BACKOFF_BASE_SECONDS * (2 ** max(0, attempt - 1))) return random.uniform(d / 2, d) def iso_now() -> str: return datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z") def add_event(conn, job_id, event: str, detail: str | None = None) -> None: conn.execute("INSERT INTO job_events(job_id,event,detail) VALUES (%s,%s,%s)", (job_id, event, detail)) def enqueue(conn, job_id, attachment_id, pipeline_version: int, attempt: int, delay_seconds: float = 0.0) -> None: """Record the intent to publish a job message in the SAME transaction as the state change (transactional outbox). The message carries only IDs (jobs.schema.json): no bytes, no credentials, no URLs. Lab 6: the W3C trace context of the CURRENT span is stored in the payload (traceparent/tracestate), so the causal link survives the outbox -> dispatcher -> Kafka -> worker hops (and the retry loop: worker -> enqueue -> ...). """ payload = {"schema_version": 1, "job_id": str(job_id), "type": JOB_TYPE, "attachment_id": str(attachment_id), "pipeline_version": pipeline_version, "attempt": attempt, "created_at": iso_now()} with trace.get_tracer("messenger").start_as_current_span( "outbox.enqueue", attributes={"messaging.system": "kafka", "messaging.destination.name": JOBS_TOPIC, "job.attempt": attempt}): inject(payload) # adds "traceparent" (+ "tracestate" when present): carrier = the JSON payload itself conn.execute( "INSERT INTO outbox(job_id,topic,msg_key,payload,available_at) VALUES (%s,%s,%s,%s::jsonb, now() + make_interval(secs => %s))", (job_id, JOBS_TOPIC, str(attachment_id), json.dumps(payload), delay_seconds)) def new_job_id() -> uuid.UUID: return uuid.uuid4() def fail_job(conn, job_id, att_id, code: str, attempt: int) -> None: conn.execute("UPDATE attachment_jobs SET status='failed', error_code=%s, lease_until=NULL, updated_at=now() WHERE job_id=%s", (code, job_id)) conn.execute("UPDATE attachments SET status='failed', error_code=%s, updated_at=now() WHERE id=%s", (code, att_id)) add_event(conn, job_id, "failed", code)