forked from mtusi2026/mtusi2026lab5
Лаба 5: Redis Streams очередь + асинхронная обработка изображений (оценка 4)
Переиспользован стек lab4 (LB/2 реплики/Postgres/SeaweedFS). upload теперь 202 queued; отдельный worker-процесс генерирует thumbnail/crop/optimized. Transactional outbox закрывает разрыв COMMIT/publish; durable retry (job_retry_schedule) переживает падение worker во время backoff; lease recovery через XAUTOCLAIM; DLQ-реестр (job_failures) с явным operator replay (scripts/replay_failed.py). make test/test-persistence/test-failover/ validate зелёные; в процессе экспериментов найдены и исправлены два реальных бага (неполный try/except вокруг S3-записи вариантов, ошибка типов asyncpg в построении interval) — см. REPORT.md и evidence/. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""Private S3-compatible object storage for attachment bytes (MinIO behind the scenes).
|
||||
|
||||
boto3 is synchronous; calls are pushed to a thread so the event loop is not
|
||||
blocked by S3 I/O. Object keys are server-generated UUIDs, never the
|
||||
user-supplied filename (filename is display-only, see contracts/README.md).
|
||||
"""
|
||||
import asyncio
|
||||
import io
|
||||
import uuid
|
||||
|
||||
import boto3
|
||||
from botocore.client import Config
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
_client = None
|
||||
_bucket: str | None = None
|
||||
|
||||
|
||||
def init(endpoint: str, region: str, access_key: str, secret_key: str, bucket: str) -> None:
|
||||
global _client, _bucket
|
||||
_bucket = bucket
|
||||
_client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=endpoint,
|
||||
region_name=region,
|
||||
aws_access_key_id=access_key,
|
||||
aws_secret_access_key=secret_key,
|
||||
config=Config(signature_version="s3v4", retries={"max_attempts": 2}),
|
||||
)
|
||||
|
||||
|
||||
async def ensure_bucket() -> None:
|
||||
"""The API identity only has Read/Write/List on this one bucket, not
|
||||
bucket-admin rights, so CreateBucket is not an option. SeaweedFS (like S3)
|
||||
auto-creates the bucket on the first successful PutObject, so writing an
|
||||
empty marker object both creates the bucket and proves write access."""
|
||||
await asyncio.to_thread(_client.put_object, Bucket=_bucket, Key=".bootstrap", Body=b"")
|
||||
|
||||
|
||||
async def ping() -> bool:
|
||||
try:
|
||||
await asyncio.to_thread(_client.head_object, Bucket=_bucket, Key=".bootstrap")
|
||||
return True
|
||||
except Exception: # noqa: BLE001 - readiness probe
|
||||
return False
|
||||
|
||||
|
||||
def new_object_key() -> str:
|
||||
return f"attachments/{uuid.uuid4().hex}"
|
||||
|
||||
|
||||
async def put_object(key: str, data: bytes, content_type: str) -> None:
|
||||
await asyncio.to_thread(
|
||||
_client.put_object, Bucket=_bucket, Key=key, Body=data, ContentType=content_type
|
||||
)
|
||||
|
||||
|
||||
async def get_object(key: str) -> bytes:
|
||||
def _get():
|
||||
resp = _client.get_object(Bucket=_bucket, Key=key)
|
||||
return resp["Body"].read()
|
||||
|
||||
return await asyncio.to_thread(_get)
|
||||
Reference in New Issue
Block a user