feat: implement background worker for thumbnail generation
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import asyncio
|
||||
import json
|
||||
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 .db.models import Attachment
|
||||
from .s3_client import s3, BUCKET
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL")
|
||||
engine = create_async_engine(DATABASE_URL)
|
||||
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
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"]
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
att = await session.get(Attachment, att_id)
|
||||
if not att:
|
||||
return
|
||||
|
||||
try:
|
||||
# получаем оригинал
|
||||
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")
|
||||
|
||||
# делаем 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
|
||||
thumb_key = f"{s3_key}_thumb.jpg"
|
||||
s3.put_object(Bucket=BUCKET, Key=thumb_key, Body=thumb_data, ContentType="image/jpeg")
|
||||
|
||||
# обновляем статус и метаданные
|
||||
thumb_sha = hashlib.sha256(thumb_data).hexdigest()
|
||||
att.status = "ready"
|
||||
att.error_code = None
|
||||
att.variants = [{
|
||||
"name": "thumbnail",
|
||||
"content_type": "image/jpeg",
|
||||
"width": thumb.width,
|
||||
"height": thumb.height,
|
||||
"size_bytes": len(thumb_data),
|
||||
"sha256": thumb_sha
|
||||
}]
|
||||
att.updated_at = datetime.now(timezone.utc)
|
||||
await session.commit()
|
||||
|
||||
except Exception as e:
|
||||
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}")
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
task = json.loads(body)
|
||||
asyncio.run(process_attachment(task))
|
||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
def main():
|
||||
connection = pika.BlockingConnection(pika.URLParameters(RABBITMQ_URL))
|
||||
channel = connection.channel()
|
||||
channel.queue_declare(queue=QUEUE_NAME, durable=True)
|
||||
channel.basic_qos(prefetch_count=1)
|
||||
channel.basic_consume(queue=QUEUE_NAME, on_message_callback=callback)
|
||||
print("Worker started, waiting for messages...")
|
||||
channel.start_consuming()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user