2024-03-06 10:28:34 -08:00
|
|
|
import json
|
2023-08-30 09:29:08 -07:00
|
|
|
import os
|
2023-05-04 07:56:24 -07:00
|
|
|
|
2016-06-03 14:54:46 +01:00
|
|
|
from flask import current_app
|
2016-10-13 15:27:47 +01:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-06-03 14:54:46 +01:00
|
|
|
|
2024-12-13 15:04:18 -08:00
|
|
|
from app import notify_celery, redis_store
|
2020-12-30 17:06:49 +00:00
|
|
|
from app.clients.email import EmailClientNonRetryableException
|
2020-08-13 17:18:19 +01:00
|
|
|
from app.clients.email.aws_ses import AwsSesClientThrottlingSendRateException
|
2021-01-18 15:43:50 +00:00
|
|
|
from app.clients.sms import SmsClientResponseException
|
2024-12-04 07:37:59 -08:00
|
|
|
from app.config import Config, QueueNames
|
2016-09-22 09:16:58 +01:00
|
|
|
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
|
2016-09-20 17:24:28 +01:00
|
|
|
from app.delivery import send_to_providers
|
2024-01-16 15:47:55 -05:00
|
|
|
from app.enums import NotificationStatus
|
2018-03-16 17:18:44 +00:00
|
|
|
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
|
|
|
|
2016-09-22 17:18:05 +01:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@notify_celery.task(
|
|
|
|
|
bind=True, name="deliver_sms", max_retries=48, default_retry_delay=300
|
|
|
|
|
)
|
2016-09-21 13:27:32 +01:00
|
|
|
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."""
|
2016-09-21 13:27:32 +01:00
|
|
|
try:
|
2016-09-22 09:16:58 +01:00
|
|
|
notification = notifications_dao.get_notification_by_id(notification_id)
|
2023-08-30 09:29:08 -07:00
|
|
|
ansi_green = "\033[32m"
|
|
|
|
|
ansi_reset = "\033[0m"
|
|
|
|
|
|
2016-09-22 09:52:23 +01:00
|
|
|
if not notification:
|
|
|
|
|
raise NoResultFound()
|
2023-08-30 09:29:08 -07:00
|
|
|
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
|
|
|
|
|
)
|
2024-03-18 11:32:29 -06:00
|
|
|
# Code branches off to send_to_providers.py
|
2024-12-13 15:04:18 -08:00
|
|
|
send_to_providers.send_sms_to_provider(notification)
|
2024-12-13 07:30:41 -08:00
|
|
|
|
2025-01-23 13:28:26 -08:00
|
|
|
cache_key = total_limit_cache_key(notification.service_id)
|
|
|
|
|
redis_store.incr(cache_key)
|
|
|
|
|
|
2021-01-18 15:43:50 +00:00
|
|
|
except Exception as e:
|
2023-10-02 14:09:50 -07:00
|
|
|
update_notification_status_by_id(
|
2024-01-16 07:37:21 -05:00
|
|
|
notification_id,
|
|
|
|
|
NotificationStatus.TEMPORARY_FAILURE,
|
2023-10-02 14:09:50 -07:00
|
|
|
)
|
2021-01-18 15:43:50 +00:00
|
|
|
if isinstance(e, SmsClientResponseException):
|
|
|
|
|
current_app.logger.warning(
|
2022-03-25 12:35:22 +00:00
|
|
|
"SMS notification delivery for id: {} failed".format(notification_id),
|
2021-01-18 15:43:50 +00:00
|
|
|
)
|
|
|
|
|
else:
|
2016-12-15 17:30:05 +00:00
|
|
|
current_app.logger.exception(
|
2024-08-15 11:07:36 -07:00
|
|
|
"SMS notification delivery for id: {} failed".format(notification_id),
|
2016-09-21 13:27:32 +01:00
|
|
|
)
|
2021-01-18 15:43:50 +00:00
|
|
|
|
|
|
|
|
try:
|
2019-08-07 14:27:58 +01:00
|
|
|
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,
|
|
|
|
|
)
|
2019-08-07 14:27:58 +01:00
|
|
|
else:
|
2024-12-04 07:37:59 -08:00
|
|
|
self.retry(
|
|
|
|
|
queue=QueueNames.RETRY, expires=Config.DEFAULT_REDIS_EXPIRE_TIME
|
|
|
|
|
)
|
2016-09-21 13:27:32 +01:00
|
|
|
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(
|
2024-01-16 07:37:21 -05:00
|
|
|
notification_id,
|
|
|
|
|
NotificationStatus.TECHNICAL_FAILURE,
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2018-03-16 17:18:44 +00:00
|
|
|
raise NotificationTechnicalFailureException(message)
|
2016-09-21 13:27:32 +01:00
|
|
|
|
|
|
|
|
|
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
|
|
|
)
|
2016-09-21 13:27:32 +01: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)
|
|
|
|
|
)
|
2016-09-22 09:16:58 +01:00
|
|
|
notification = notifications_dao.get_notification_by_id(notification_id)
|
2024-03-06 10:28:34 -08:00
|
|
|
|
2016-09-22 09:52:23 +01: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:
|
2024-09-11 09:39:18 -07:00
|
|
|
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")
|
2020-08-13 17:18:19 +01:00
|
|
|
except Exception as e:
|
2016-09-21 13:27:32 +01:00
|
|
|
try:
|
2020-08-13 17:18:19 +01:00
|
|
|
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)
|
2016-09-21 13:27:32 +01:00
|
|
|
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(
|
2024-01-16 07:37:21 -05:00
|
|
|
notification_id,
|
|
|
|
|
NotificationStatus.TECHNICAL_FAILURE,
|
2023-08-29 14:54:30 -07:00
|
|
|
)
|
2018-03-16 17:18:44 +00:00
|
|
|
raise NotificationTechnicalFailureException(message)
|