diff --git a/app/celery/provider_tasks.py b/app/celery/provider_tasks.py index b0df268b3..48f5ea2fb 100644 --- a/app/celery/provider_tasks.py +++ b/app/celery/provider_tasks.py @@ -5,6 +5,7 @@ from sqlalchemy.orm.exc import NoResultFound from app import notify_celery from app.dao import notifications_dao from app.dao.notifications_dao import update_notification_status_by_id +from app.dao.provider_details_dao import dao_toggle_sms_provider from app.statsd_decorators import statsd from app.delivery import send_to_providers @@ -43,6 +44,7 @@ def deliver_sms(self, notification_id): send_to_providers.send_sms_to_provider(notification) except Exception as e: try: + dao_toggle_sms_provider() current_app.logger.exception( "RETRY: SMS notification {} failed".format(notification_id) ) diff --git a/tests/app/celery/test_provider_tasks.py b/tests/app/celery/test_provider_tasks.py index 7271e9a4c..c9f5c29d9 100644 --- a/tests/app/celery/test_provider_tasks.py +++ b/tests/app/celery/test_provider_tasks.py @@ -121,3 +121,13 @@ def test_should_technical_error_and_not_retry_if_invalid_email(sample_notificati assert provider_tasks.deliver_email.retry.called is False assert sample_notification.status == 'technical-failure' + + +def test_send_sms_should_switch_providers_on_provider_failure(sample_notification, mocker): + mocker.patch('app.delivery.send_to_providers.send_sms_to_provider', side_effect=Exception("Provider Exception")) + switch_provider_mock = mocker.patch('app.celery.provider_tasks.dao_toggle_sms_provider') + mocker.patch('app.celery.provider_tasks.deliver_sms.retry') + + deliver_sms(sample_notification.service_id) + + assert switch_provider_mock.called is True