mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:15:19 -05:00
Handle errors where the notification isn't found when executing the tasks
- Thows a NoResultFound sqlalchemy exception - Which causes a retry. This means we give it a few goes (5, max 5 hours) for the notification to appear. - Should never happen, only if we get some task overlaps that are unusual that leads to tasks executed in an overlapping nature.
This commit is contained in:
@@ -6,7 +6,7 @@ from app.dao.notifications_dao import update_notification_status_by_id
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
from app.delivery import send_to_providers
|
||||
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
def retry_iteration_to_delay(retry=0):
|
||||
"""
|
||||
@@ -37,6 +37,8 @@ def retry_iteration_to_delay(retry=0):
|
||||
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:
|
||||
@@ -58,6 +60,8 @@ def deliver_sms(self, notification_id):
|
||||
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)
|
||||
except Exception as e:
|
||||
try:
|
||||
@@ -78,7 +82,10 @@ def deliver_email(self, notification_id):
|
||||
@statsd(namespace="tasks")
|
||||
def send_sms_to_provider(self, service_id, notification_id):
|
||||
try:
|
||||
send_to_providers.send_sms_to_provider(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:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
@@ -98,7 +105,10 @@ def send_sms_to_provider(self, service_id, notification_id):
|
||||
@statsd(namespace="tasks")
|
||||
def send_email_to_provider(self, service_id, notification_id):
|
||||
try:
|
||||
send_to_providers.send_email_response(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 Exception as e:
|
||||
try:
|
||||
current_app.logger.error(
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from celery.exceptions import MaxRetriesExceededError
|
||||
from app.celery import provider_tasks
|
||||
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
|
||||
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider, deliver_sms, deliver_email
|
||||
from app.clients.email import EmailClientException
|
||||
from app.models import Notification
|
||||
from tests.app.conftest import sample_notification
|
||||
import app
|
||||
|
||||
|
||||
def test_should_have_decorated_tasks_functions():
|
||||
@@ -39,12 +40,116 @@ def test_should_by_240_minute_delay_on_retry_two():
|
||||
assert provider_tasks.retry_iteration_to_delay(4) == 14400
|
||||
|
||||
|
||||
def test_should_call_send_sms_to_provider_from_deliver_sms_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_notification,
|
||||
mocker):
|
||||
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider')
|
||||
|
||||
deliver_sms(sample_notification.id)
|
||||
app.delivery.send_to_providers.send_sms_to_provider.assert_called_with(sample_notification)
|
||||
|
||||
|
||||
def test_should_add_to_retry_queue_if_notification_not_found_in_deliver_sms_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider')
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.retry')
|
||||
|
||||
notification_id = app.create_uuid()
|
||||
|
||||
deliver_sms(notification_id)
|
||||
app.delivery.send_to_providers.send_sms_to_provider.assert_not_called()
|
||||
app.celery.provider_tasks.deliver_sms.retry.assert_called_with(queue="retry", countdown=10)
|
||||
|
||||
|
||||
def test_should_call_send_sms_to_provider_from_send_sms_to_provider_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_notification,
|
||||
mocker):
|
||||
|
||||
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider')
|
||||
|
||||
send_sms_to_provider(sample_notification.service_id, sample_notification.id)
|
||||
app.delivery.send_to_providers.send_sms_to_provider.assert_called_with(sample_notification)
|
||||
|
||||
|
||||
def test_should_add_to_retry_queue_if_notification_not_found_in_send_sms_to_provider_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
mocker.patch('app.delivery.send_to_providers.send_sms_to_provider')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.retry')
|
||||
|
||||
notification_id = app.create_uuid()
|
||||
service_id = app.create_uuid()
|
||||
|
||||
send_sms_to_provider(service_id, notification_id)
|
||||
app.delivery.send_to_providers.send_sms_to_provider.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.retry.assert_called_with(queue="retry", countdown=10)
|
||||
|
||||
|
||||
def test_should_call_send_email_to_provider_from_deliver_email_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_notification,
|
||||
mocker):
|
||||
|
||||
mocker.patch('app.delivery.send_to_providers.send_email_to_provider')
|
||||
|
||||
deliver_email(sample_notification.id)
|
||||
app.delivery.send_to_providers.send_email_to_provider.assert_called_with(sample_notification)
|
||||
|
||||
|
||||
def test_should_add_to_retry_queue_if_notification_not_found_in_deliver_email_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
mocker.patch('app.delivery.send_to_providers.send_email_to_provider')
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.retry')
|
||||
|
||||
notification_id = app.create_uuid()
|
||||
|
||||
deliver_email(notification_id)
|
||||
app.delivery.send_to_providers.send_email_to_provider.assert_not_called()
|
||||
app.celery.provider_tasks.deliver_email.retry.assert_called_with(queue="retry", countdown=10)
|
||||
|
||||
|
||||
def test_should_call_send_email_to_provider_from_email_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_notification,
|
||||
mocker):
|
||||
|
||||
mocker.patch('app.delivery.send_to_providers.send_email_to_provider')
|
||||
|
||||
send_email_to_provider(sample_notification.service_id, sample_notification.id)
|
||||
app.delivery.send_to_providers.send_email_to_provider.assert_called_with(sample_notification)
|
||||
|
||||
|
||||
def test_should_add_to_retry_queue_if_notification_not_found_in_send_email_to_provider_task(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
mocker.patch('app.delivery.send_to_providers.send_email_to_provider')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.retry')
|
||||
|
||||
notification_id = app.create_uuid()
|
||||
service_id = app.create_uuid()
|
||||
|
||||
send_email_to_provider(service_id, notification_id)
|
||||
app.delivery.send_to_providers.send_email_to_provider.assert_not_called()
|
||||
app.celery.provider_tasks.send_email_to_provider.retry.assert_called_with(queue="retry", countdown=10)
|
||||
|
||||
|
||||
def test_should_go_into_technical_error_if_exceeds_retries(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
mocker):
|
||||
|
||||
notification = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
|
||||
service=sample_service, status='created')
|
||||
|
||||
@@ -68,7 +173,6 @@ def test_send_email_to_provider_should_go_into_technical_error_if_exceeds_retrie
|
||||
sample_service,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
|
||||
notification = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
|
||||
service=sample_service, status='created', template=sample_email_template)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user