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

56 lines
2.3 KiB
Python
Raw 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 notifications_utils.recipients import InvalidEmailError
from sqlalchemy.orm.exc import NoResultFound
2016-09-20 17:24:28 +01:00
from app import notify_celery
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
2016-08-05 10:44:43 +01:00
from app.statsd_decorators import statsd
2016-09-20 17:24:28 +01:00
from app.delivery import send_to_providers
2017-05-25 11:12:40 +01:00
@notify_celery.task(bind=True, name="deliver_sms", max_retries=48, default_retry_delay=300)
@statsd(namespace="tasks")
def deliver_sms(self, notification_id):
try:
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:
try:
2016-12-15 17:30:05 +00:00
current_app.logger.exception(
2017-04-27 16:58:00 +01:00
"SMS notification delivery for id: {} failed".format(notification_id)
)
2017-05-25 11:12:40 +01:00
self.retry(queue=QueueNames.RETRY)
except self.MaxRetriesExceededError:
2016-12-15 17:30:05 +00:00
current_app.logger.exception(
"RETRY FAILED: task send_sms_to_provider failed for notification {}".format(notification_id),
)
update_notification_status_by_id(notification_id, 'technical-failure')
2017-05-25 11:12:40 +01:00
@notify_celery.task(bind=True, name="deliver_email", max_retries=48, default_retry_delay=300)
@statsd(namespace="tasks")
def deliver_email(self, notification_id):
try:
notification = notifications_dao.get_notification_by_id(notification_id)
if not notification:
raise NoResultFound()
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')
except Exception as e:
try:
2016-12-15 17:30:05 +00:00
current_app.logger.exception(
"RETRY: Email notification {} failed".format(notification_id)
)
2017-05-25 11:12:40 +01:00
self.retry(queue=QueueNames.RETRY)
except self.MaxRetriesExceededError:
current_app.logger.error(
2016-12-15 17:30:05 +00:00
"RETRY FAILED: task send_email_to_provider failed for notification {}".format(notification_id)
)
update_notification_status_by_id(notification_id, 'technical-failure')