mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 15:12:08 +01:00
29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
# Route email jobs via Celery instead of raw RabbitMQ publishing
|
|
# Ensure Celery app is initialized so producers use correct broker credentials.
|
|
# Import has side effects (sets Celery default app), safe to keep at module level.
|
|
import app.celery_app # noqa: F401
|
|
|
|
# Use a lazy proxy so _send_email_task is never None, even if Celery modules
|
|
# are not yet importable at import time (e.g., different working dir during startup).
|
|
class _LazySendTask:
|
|
def delay(self, to: str, subject: str, body: str): # type: ignore[no-untyped-def]
|
|
# Late import on first use
|
|
from app.workers.celery_tasks import send_email_fields as _send # type: ignore
|
|
return _send.delay(to, subject, body)
|
|
|
|
|
|
try:
|
|
# Try eager import for normal operation
|
|
from app.workers.celery_tasks import send_email_fields as _send_email_task # type: ignore
|
|
except ImportError: # pragma: no cover - fallback lazy import proxy
|
|
_send_email_task = _LazySendTask() # type: ignore
|
|
|
|
|
|
def enqueue_email(to: str, subject: str, body: str) -> None:
|
|
"""Enqueue an email job using Celery.
|
|
|
|
Keeps the same public API as before.
|
|
"""
|
|
_send_email_task.delay(to, subject, body)
|
|
|