Files

78 lines
3.6 KiB
Python
Raw Permalink Normal View History

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-09-20 17:24:28 +01:00
from app import notify_celery
from app.clients.email import EmailClientNonRetryableException
from app.clients.email.aws_ses import AwsSesClientThrottlingSendRateException
from app.clients.sms import SmsClientResponseException
2021-03-10 13:55:06 +00:00
from app.config import QueueNames
from app.dao import notifications_dao
2016-09-20 17:24:28 +01:00
from app.dao.notifications_dao import update_notification_status_by_id
from app.delivery import send_to_providers
from app.exceptions import NotificationTechnicalFailureException
from app.models import NOTIFICATION_TECHNICAL_FAILURE
2017-05-25 11:12:40 +01:00
@notify_celery.task(bind=True, name="deliver_sms", max_retries=48, default_retry_delay=300)
def deliver_sms(self, notification_id):
try:
2018-03-26 15:24:21 +01:00
current_app.logger.info("Start sending SMS for notification id: {}".format(notification_id))
notification = notifications_dao.get_notification_by_id(notification_id)
if not notification:
raise NoResultFound()
send_to_providers.send_sms_to_provider(notification)
except Exception as e:
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),
exc_info=True
)
else:
2016-12-15 17:30:05 +00:00
current_app.logger.exception(
2018-03-26 16:44:29 +01:00
"SMS notification delivery for id: {} failed".format(notification_id)
)
try:
if self.request.retries == 0:
self.retry(queue=QueueNames.RETRY, countdown=0)
else:
self.retry(queue=QueueNames.RETRY)
except self.MaxRetriesExceededError:
2018-03-19 14:08:38 +00: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, NOTIFICATION_TECHNICAL_FAILURE)
raise NotificationTechnicalFailureException(message)
2017-05-25 11:12:40 +01:00
@notify_celery.task(bind=True, name="deliver_email", max_retries=48, default_retry_delay=300)
def deliver_email(self, notification_id):
try:
2018-03-26 15:24:21 +01:00
current_app.logger.info("Start sending email for notification id: {}".format(notification_id))
notification = notifications_dao.get_notification_by_id(notification_id)
if not notification:
raise NoResultFound()
send_to_providers.send_email_to_provider(notification)
except EmailClientNonRetryableException as e:
2020-12-30 17:28:21 +00:00
current_app.logger.exception(
f"Email notification {notification_id} failed: {e}"
)
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"
)
2017-05-25 11:12:40 +01:00
self.retry(queue=QueueNames.RETRY)
except self.MaxRetriesExceededError:
2018-03-19 14:08:38 +00: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, NOTIFICATION_TECHNICAL_FAILURE)
raise NotificationTechnicalFailureException(message)