feat: implement transactional outbox, dlq and worker timeouts

This commit is contained in:
2026-11-12 21:35:48 +03:00
parent 067b2d866f
commit 479dacc934
7 changed files with 278 additions and 221 deletions
+60 -83
View File
@@ -4,127 +4,104 @@ import os
import io
import hashlib
from datetime import datetime, timezone
import pika
from PIL import Image, ImageOps
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.pool import NullPool
from .db.models import Attachment
from .s3_client import s3, BUCKET
from .queue import RABBITMQ_URL, QUEUE_NAME, setup_rabbitmq
DATABASE_URL = os.getenv("DATABASE_URL")
RABBITMQ_URL = os.getenv("RABBITMQ_URL", "amqp://guest:guest@rabbitmq:5672/")
QUEUE_NAME = "attachment_tasks"
async def process_attachment(task: dict):
att_id = task["attachment_id"]
s3_key = task["s3_key"]
# ВАЖНО: создаем engine внутри функции с NullPool, чтобы избежать ошибки разных event loop
engine = create_async_engine(DATABASE_URL, poolclass=NullPool)
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
try:
async with AsyncSessionLocal() as session:
att = await session.get(Attachment, att_id)
if not att:
return
if not att: return
if att.status == "ready" and len(att.variants or []) == 3: return
# идемпотентность: если уже обработано, пропускаем
if att.status == "ready" and len(att.variants or []) == 3:
return
obj = s3.get_object(Bucket=BUCKET, Key=s3_key)
content = obj["Body"].read()
img = Image.open(io.BytesIO(content))
img = ImageOps.exif_transpose(img)
if img.mode in ("RGBA", "LA"):
background = Image.new("RGB", img.size, (255, 255, 255))
background.paste(img, mask=img.split()[-1])
img = background
elif img.mode != "RGB":
img = img.convert("RGB")
try:
# получаем оригинал
obj = s3.get_object(Bucket=BUCKET, Key=s3_key)
content = obj["Body"].read()
img = Image.open(io.BytesIO(content))
# разворачиваем по exif и убираем прозрачность
img = ImageOps.exif_transpose(img)
if img.mode in ("RGBA", "LA"):
background = Image.new("RGB", img.size, (255, 255, 255))
background.paste(img, mask=img.split()[-1])
img = background
elif img.mode != "RGB":
img = img.convert("RGB")
variants_meta = []
thumb = img.copy()
thumb.thumbnail((256, 256), Image.Resampling.LANCZOS)
thumb_bytes = io.BytesIO()
thumb.save(thumb_bytes, format="JPEG")
thumb_data = thumb_bytes.getvalue()
s3.put_object(Bucket=BUCKET, Key=f"{s3_key}_thumb.jpg", Body=thumb_data, ContentType="image/jpeg")
variants_meta.append({"name": "thumbnail", "content_type": "image/jpeg", "width": thumb.width, "height": thumb.height, "size_bytes": len(thumb_data), "sha256": hashlib.sha256(thumb_data).hexdigest()})
variants_meta = []
crop_img = img.copy()
width, height = crop_img.size
min_dim = min(width, height)
crop_img = crop_img.crop(((width - min_dim) / 2, (height - min_dim) / 2, (width + min_dim) / 2, (height + min_dim) / 2))
crop_img = crop_img.resize((128, 128), Image.Resampling.LANCZOS)
crop_bytes = io.BytesIO()
crop_img.save(crop_bytes, format="JPEG")
crop_data = crop_bytes.getvalue()
s3.put_object(Bucket=BUCKET, Key=f"{s3_key}_crop.jpg", Body=crop_data, ContentType="image/jpeg")
variants_meta.append({"name": "crop", "content_type": "image/jpeg", "width": 128, "height": 128, "size_bytes": len(crop_data), "sha256": hashlib.sha256(crop_data).hexdigest()})
# 1. Thumbnail (256x256)
thumb = img.copy()
thumb.thumbnail((256, 256), Image.Resampling.LANCZOS)
thumb_bytes = io.BytesIO()
thumb.save(thumb_bytes, format="JPEG")
thumb_data = thumb_bytes.getvalue()
s3.put_object(Bucket=BUCKET, Key=f"{s3_key}_thumb.jpg", Body=thumb_data, ContentType="image/jpeg")
variants_meta.append({
"name": "thumbnail", "content_type": "image/jpeg",
"width": thumb.width, "height": thumb.height,
"size_bytes": len(thumb_data), "sha256": hashlib.sha256(thumb_data).hexdigest()
})
opt_img = img.copy()
opt_img.thumbnail((1280, 1280), Image.Resampling.LANCZOS)
opt_bytes = io.BytesIO()
opt_img.save(opt_bytes, format="WEBP", quality=85)
opt_data = opt_bytes.getvalue()
s3.put_object(Bucket=BUCKET, Key=f"{s3_key}_opt.webp", Body=opt_data, ContentType="image/webp")
variants_meta.append({"name": "optimized", "content_type": "image/webp", "width": opt_img.width, "height": opt_img.height, "size_bytes": len(opt_data), "sha256": hashlib.sha256(opt_data).hexdigest()})
# 2. Crop (128x128)
crop_img = img.copy()
width, height = crop_img.size
min_dim = min(width, height)
left = (width - min_dim) / 2
top = (height - min_dim) / 2
right = (width + min_dim) / 2
bottom = (height + min_dim) / 2
crop_img = crop_img.crop((left, top, right, bottom))
crop_img = crop_img.resize((128, 128), Image.Resampling.LANCZOS)
crop_bytes = io.BytesIO()
crop_img.save(crop_bytes, format="JPEG")
crop_data = crop_bytes.getvalue()
s3.put_object(Bucket=BUCKET, Key=f"{s3_key}_crop.jpg", Body=crop_data, ContentType="image/jpeg")
variants_meta.append({
"name": "crop", "content_type": "image/jpeg",
"width": 128, "height": 128,
"size_bytes": len(crop_data), "sha256": hashlib.sha256(crop_data).hexdigest()
})
# 3. Optimized (WebP)
opt_img = img.copy()
opt_img.thumbnail((1280, 1280), Image.Resampling.LANCZOS)
opt_bytes = io.BytesIO()
opt_img.save(opt_bytes, format="WEBP", quality=85)
opt_data = opt_bytes.getvalue()
s3.put_object(Bucket=BUCKET, Key=f"{s3_key}_opt.webp", Body=opt_data, ContentType="image/webp")
variants_meta.append({
"name": "optimized", "content_type": "image/webp",
"width": opt_img.width, "height": opt_img.height,
"size_bytes": len(opt_data), "sha256": hashlib.sha256(opt_data).hexdigest()
})
att.status = "ready"
att.error_code = None
att.variants = variants_meta
att.updated_at = datetime.now(timezone.utc)
await session.commit()
except Exception as e:
att.status = "ready"
att.error_code = None
att.variants = variants_meta
att.updated_at = datetime.now(timezone.utc)
await session.commit()
except Exception as e:
async with AsyncSessionLocal() as session:
att = await session.get(Attachment, att_id)
if att:
att.status = "failed"
att.error_code = "processing_failed"
att.updated_at = datetime.now(timezone.utc)
await session.commit()
print(f"Error processing {att_id}: {e}", flush=True)
print(f"Error processing {att_id}: {e}", flush=True)
raise e
finally:
await engine.dispose()
def callback(ch, method, properties, body):
task = json.loads(body)
asyncio.run(process_attachment(task))
ch.basic_ack(delivery_tag=method.delivery_tag)
try:
# Таймаут 30 секунд
asyncio.run(asyncio.wait_for(process_attachment(task), timeout=30.0))
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception as e:
print(f"Task failed, routing to DLQ. Error: {e}", flush=True)
# requeue=False отправляет в DLQ
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=False)
def main():
connection = pika.BlockingConnection(pika.URLParameters(RABBITMQ_URL))
channel = connection.channel()
channel.queue_declare(queue=QUEUE_NAME, durable=True)
setup_rabbitmq(channel)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue=QUEUE_NAME, on_message_callback=callback)
print("Worker started, waiting for messages...", flush=True)
print("Worker started...", flush=True)
channel.start_consuming()
if __name__ == "__main__":