Files
notifications-api/app/celery/provider_tasks.py

132 lines
5.1 KiB
Python
Raw Normal View History

2024-03-06 10:28:34 -08:00
import json
import os
2023-05-04 07:56:24 -07:00
from flask import current_app
from sqlalchemy.orm.exc import NoResultFound
2024-12-13 15:04:18 -08:00
from app import notify_celery, redis_store
from app.clients.email import EmailClientNonRetryableException
from app.clients.email.aws_ses import AwsSesClientThrottlingSendRateException
from app.clients.sms import SmsClientResponseException
2024-12-04 07:37:59 -08:00
from app.config import Config, QueueNames
from app.dao import notifications_dao
2024-12-13 15:04:18 -08:00
from app.dao.notifications_dao import update_notification_status_by_id
from app.delivery import send_to_providers
from app.enums import NotificationStatus
from app.exceptions import NotificationTechnicalFailureException
2025-01-23 13:28:26 -08:00
from notifications_utils.clients.redis import total_limit_cache_key
2023-05-26 13:47:05 -07:00
2023-08-29 14:54:30 -07:00
@notify_celery.task(
bind=True, name="deliver_sms", max_retries=48, default_retry_delay=300
)
def deliver_sms(self, notification_id):
2024-03-27 10:32:40 -06:00
"""Branch off to the final step in delivering the notification to sns and get delivery receipts."""
try:
notification = notifications_dao.get_notification_by_id(notification_id)
ansi_green = "\033[32m"
ansi_reset = "\033[0m"
if not notification:
raise NoResultFound()
if (
os.getenv("NOTIFY_ENVIRONMENT") == "development"
and "authentication code" in notification.content
):
current_app.logger.warning(
ansi_green + f"AUTHENTICATION CODE: {notification.content}" + ansi_reset
)
# Code branches off to send_to_providers.py
2024-12-13 15:04:18 -08:00
send_to_providers.send_sms_to_provider(notification)
2025-01-23 13:28:26 -08:00
cache_key = total_limit_cache_key(notification.service_id)
redis_store.incr(cache_key)
except Exception as e:
update_notification_status_by_id(
notification_id,
NotificationStatus.TEMPORARY_FAILURE,
)
if isinstance(e, SmsClientResponseException):
current_app.logger.warning(
"SMS notification delivery for id: {} failed".format(notification_id),
)
else:
current_app.logger.exception(
2024-08-15 11:07:36 -07:00
"SMS notification delivery for id: {} failed".format(notification_id),
)
try:
if self.request.retries == 0:
2024-12-04 07:37:59 -08:00
self.retry(
queue=QueueNames.RETRY,
countdown=0,
expires=Config.DEFAULT_REDIS_EXPIRE_TIME,
)
else:
2024-12-04 07:37:59 -08:00
self.retry(
queue=QueueNames.RETRY, expires=Config.DEFAULT_REDIS_EXPIRE_TIME
)
except self.MaxRetriesExceededError:
2023-08-29 14:54:30 -07:00
message = (
"RETRY FAILED: Max retries reached. The task send_sms_to_provider failed for notification {}. "
"Notification has been updated to technical-failure".format(
notification_id
)
)
update_notification_status_by_id(
notification_id,
NotificationStatus.TECHNICAL_FAILURE,
2023-08-29 14:54:30 -07:00
)
raise NotificationTechnicalFailureException(message)
2023-08-29 14:54:30 -07:00
@notify_celery.task(
2024-11-27 08:36:52 -08:00
bind=True, name="deliver_email", max_retries=48, default_retry_delay=30
2023-08-29 14:54:30 -07:00
)
def deliver_email(self, notification_id):
try:
2023-08-29 14:54:30 -07:00
current_app.logger.info(
"Start sending email for notification id: {}".format(notification_id)
)
notification = notifications_dao.get_notification_by_id(notification_id)
2024-03-06 10:28:34 -08:00
if not notification:
raise NoResultFound()
2024-03-06 10:28:34 -08:00
personalisation = redis_store.get(f"email-personalisation-{notification_id}")
2024-11-27 08:36:52 -08:00
recipient = redis_store.get(f"email-recipient-{notification_id}")
if personalisation:
notification.personalisation = json.loads(personalisation)
if recipient:
notification.recipient = json.loads(recipient)
2024-03-06 10:28:34 -08:00
2024-11-27 08:50:12 -08:00
send_to_providers.send_email_to_provider(notification)
2024-08-15 11:24:24 -07:00
except EmailClientNonRetryableException:
current_app.logger.exception(f"Email notification {notification_id} failed")
2023-08-29 14:54:30 -07:00
update_notification_status_by_id(notification_id, "technical-failure")
except Exception as e:
try:
if isinstance(e, AwsSesClientThrottlingSendRateException):
current_app.logger.warning(
f"RETRY: Email notification {notification_id} was rate limited by SES"
)
else:
current_app.logger.exception(
f"RETRY: Email notification {notification_id} failed"
)
2024-12-04 07:37:59 -08:00
self.retry(queue=QueueNames.RETRY, expires=Config.DEFAULT_REDIS_EXPIRE_TIME)
except self.MaxRetriesExceededError:
2023-08-29 14:54:30 -07:00
message = (
"RETRY FAILED: Max retries reached. "
"The task send_email_to_provider failed for notification {}. "
"Notification has been updated to technical-failure".format(
notification_id
)
)
update_notification_status_by_id(
notification_id,
NotificationStatus.TECHNICAL_FAILURE,
2023-08-29 14:54:30 -07:00
)
raise NotificationTechnicalFailureException(message)