mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 15:12:08 +01:00
feat(infrastructure): use celery worker
This commit is contained in:
44
backend/app/workers/celery_tasks.py
Normal file
44
backend/app/workers/celery_tasks.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import logging
|
||||
from typing import Any, Dict
|
||||
|
||||
from celery import shared_task
|
||||
|
||||
logger = logging.getLogger("celery_tasks")
|
||||
if not logger.handlers:
|
||||
_h = logging.StreamHandler()
|
||||
logger.addHandler(_h)
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
|
||||
@shared_task(name="workers.send_email")
|
||||
def send_email(payload: Dict[str, Any]) -> None:
|
||||
"""Celery task to send an email.
|
||||
|
||||
Expected payload schema:
|
||||
{
|
||||
"type": "email",
|
||||
"to": "recipient@example.com",
|
||||
"subject": "Subject text",
|
||||
"body": "Email body text"
|
||||
}
|
||||
|
||||
This mirrors the previous queue worker contract to minimize changes upstream.
|
||||
"""
|
||||
to = payload.get("to")
|
||||
subject = payload.get("subject")
|
||||
body = payload.get("body")
|
||||
if not (to and subject and body):
|
||||
logger.error("Email task missing fields. Payload: %s", payload)
|
||||
return
|
||||
|
||||
# Placeholder for real email sending logic
|
||||
# Implement SMTP provider call here
|
||||
logger.info("[Celery] Email sent | to=%s | subject=%s | body_len=%d", to, subject, len(body))
|
||||
|
||||
|
||||
@shared_task(name="workers.send_email_fields")
|
||||
def send_email_fields(to: str, subject: str, body: str) -> None:
|
||||
"""Alternate signature for convenience when calling from app code.
|
||||
Routes to the same implementation as send_email.
|
||||
"""
|
||||
send_email({"type": "email", "to": to, "subject": subject, "body": body})
|
||||
Reference in New Issue
Block a user