infrasynth-backend-kit/infrasynth/notifications/tasks.py
2026-08-28 14:38:47 -05:00

55 lines
1.7 KiB
Python

import logging
from celery import shared_task
logger = logging.getLogger(__name__)
@shared_task(
name="infrasynth.notifications.deliver_notification",
bind=True,
max_retries=3,
default_retry_delay=60,
)
def deliver_notification(self, dispatch_id):
from .models import NotificationDispatch
from .services import NotificationService
try:
dispatch = NotificationDispatch.objects.select_related("template").get(pk=dispatch_id)
except NotificationDispatch.DoesNotExist:
logger.warning("Notification dispatch %s not found", dispatch_id)
return None
service = NotificationService()
try:
service._execute_send(dispatch)
except Exception as exc: # noqa: BLE001
logger.exception("Dispatch %s failed", dispatch_id)
raise self.retry(exc=exc, countdown=60) from exc
return dispatch_id
@shared_task(
name="infrasynth.notifications.deliver_notification_with_failover",
bind=True,
max_retries=3,
default_retry_delay=60,
)
def deliver_notification_with_failover(self, dispatch_id, channels):
from .models import NotificationDispatch
from .services import NotificationService
try:
dispatch = NotificationDispatch.objects.select_related("template").get(pk=dispatch_id)
except NotificationDispatch.DoesNotExist:
logger.warning("Notification dispatch %s not found", dispatch_id)
return None
service = NotificationService()
try:
service._execute_failover(dispatch, channels)
except Exception as exc: # noqa: BLE001
logger.exception("Dispatch %s failover failed", dispatch_id)
raise self.retry(exc=exc, countdown=60) from exc
return dispatch_id