From 144356f09623a40837dd403f941c694292bf988e Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 12 Mar 2018 12:15:03 +0000 Subject: [PATCH] Fix the bug in timeout notifications. When the notification is timedout by the scheduled task if the service is expecting a status update, that update to the service would fail. A test has been added. --- app/celery/scheduled_tasks.py | 15 +++++++-------- tests/app/celery/test_scheduled_tasks.py | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index 2e3e725d4..bb23f7f1d 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -199,16 +199,15 @@ def delete_invitations(): def timeout_notifications(): notifications = dao_timeout_notifications(current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD')) - if notifications: - for notification in notifications: - # queue callback task only if the service_callback_api exists - service_callback_api = get_service_callback_api_for_service(service_id=notification.service_id) + for notification in notifications: + # queue callback task only if the service_callback_api exists + service_callback_api = get_service_callback_api_for_service(service_id=notification.service_id) - if service_callback_api: - send_delivery_status_to_service.apply_async([str(id)], queue=QueueNames.CALLBACKS) + if service_callback_api: + send_delivery_status_to_service.apply_async([str(notification.id)], queue=QueueNames.CALLBACKS) - current_app.logger.info( - "Timeout period reached for {} notifications, status has been updated.".format(len(notifications))) + current_app.logger.info( + "Timeout period reached for {} notifications, status has been updated.".format(len(notifications))) @notify_celery.task(name='send-daily-performance-platform-stats') diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index 40023c92d..b14b3fdbb 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -59,7 +59,10 @@ from app.models import ( ) from app.utils import get_london_midnight_in_utc from app.v2.errors import JobIncompleteError -from tests.app.db import create_notification, create_service, create_template, create_job, create_rate +from tests.app.db import ( + create_notification, create_service, create_template, create_job, create_rate, + create_service_callback_api +) from tests.app.conftest import ( sample_job as create_sample_job, @@ -207,6 +210,18 @@ def test_should_not_update_status_of_letter_notifications(client, sample_letter_ assert not2.status == 'created' +def test_timeout_notifications_sends_status_update_to_service(client, sample_template, mocker): + create_service_callback_api(service=sample_template.service) + mocked = mocker.patch('app.celery.service_callback_tasks.send_delivery_status_to_service.apply_async') + notification = create_notification( + template=sample_template, + status='sending', + created_at=datetime.utcnow() - timedelta( + seconds=current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD') + 10)) + timeout_notifications() + mocked.assert_called_once_with([str(notification.id)], queue=QueueNames.CALLBACKS) + + def test_should_update_scheduled_jobs_and_put_on_queue(notify_db, notify_db_session, mocker): mocked = mocker.patch('app.celery.tasks.process_job.apply_async')