2016-06-03 14:54:46 +01:00
|
|
|
from flask import current_app
|
2016-10-13 15:27:47 +01:00
|
|
|
from notifications_utils.recipients import InvalidEmailError
|
2018-02-06 09:35:33 +00:00
|
|
|
from notifications_utils.statsd_decorators import statsd
|
2016-10-13 15:27:47 +01:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-06-03 14:54:46 +01:00
|
|
|
|
2016-09-20 17:24:28 +01:00
|
|
|
from app import notify_celery
|
2017-07-19 13:50:29 +01:00
|
|
|
from app.config import QueueNames
|
2016-09-22 09:16:58 +01:00
|
|
|
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
|
2018-03-16 17:18:44 +00:00
|
|
|
from app.exceptions import NotificationTechnicalFailureException
|
|
|
|
|
from app.models import NOTIFICATION_TECHNICAL_FAILURE
|
2016-06-13 16:40:46 +01:00
|
|
|
|
2016-09-22 17:18:05 +01:00
|
|
|
|
2017-05-25 11:12:40 +01:00
|
|
|
@notify_celery.task(bind=True, name="deliver_sms", max_retries=48, default_retry_delay=300)
|
2016-09-21 13:27:32 +01:00
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
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))
|
2016-09-22 09:16:58 +01:00
|
|
|
notification = notifications_dao.get_notification_by_id(notification_id)
|
2016-09-22 09:52:23 +01:00
|
|
|
if not notification:
|
|
|
|
|
raise NoResultFound()
|
2016-09-22 09:16:58 +01:00
|
|
|
send_to_providers.send_sms_to_provider(notification)
|
2018-11-07 13:39:08 +00:00
|
|
|
except Exception:
|
2016-09-21 13:27:32 +01:00
|
|
|
try:
|
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)
|
2016-09-21 13:27:32 +01:00
|
|
|
)
|
2017-05-25 11:12:40 +01:00
|
|
|
self.retry(queue=QueueNames.RETRY)
|
2016-09-21 13:27:32 +01:00
|
|
|
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 {}. " \
|
2018-03-16 17:18:44 +00:00
|
|
|
"Notification has been updated to technical-failure".format(notification_id)
|
|
|
|
|
update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE)
|
|
|
|
|
raise NotificationTechnicalFailureException(message)
|
2016-09-21 13:27:32 +01:00
|
|
|
|
|
|
|
|
|
2017-05-25 11:12:40 +01:00
|
|
|
@notify_celery.task(bind=True, name="deliver_email", max_retries=48, default_retry_delay=300)
|
2016-09-21 13:27:32 +01:00
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
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))
|
2016-09-22 09:16:58 +01:00
|
|
|
notification = notifications_dao.get_notification_by_id(notification_id)
|
2016-09-22 09:52:23 +01:00
|
|
|
if not notification:
|
|
|
|
|
raise NoResultFound()
|
2016-09-22 09:16:58 +01:00
|
|
|
send_to_providers.send_email_to_provider(notification)
|
2016-10-13 15:27:47 +01:00
|
|
|
except InvalidEmailError as e:
|
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
|
update_notification_status_by_id(notification_id, 'technical-failure')
|
2018-11-07 13:39:08 +00:00
|
|
|
except Exception:
|
2016-09-21 13:27:32 +01:00
|
|
|
try:
|
2016-12-15 17:30:05 +00:00
|
|
|
current_app.logger.exception(
|
2018-03-26 16:44:29 +01:00
|
|
|
"RETRY: Email notification {} failed".format(notification_id)
|
2016-09-21 13:27:32 +01:00
|
|
|
)
|
2017-05-25 11:12:40 +01:00
|
|
|
self.retry(queue=QueueNames.RETRY)
|
2016-09-21 13:27:32 +01:00
|
|
|
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 {}. " \
|
2018-03-16 17:18:44 +00:00
|
|
|
"Notification has been updated to technical-failure".format(notification_id)
|
|
|
|
|
update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE)
|
|
|
|
|
raise NotificationTechnicalFailureException(message)
|