From be904559446c587be84cadbb06eba493b8899960 Mon Sep 17 00:00:00 2001 From: Toby Lorne Date: Mon, 26 Oct 2020 17:34:59 +0000 Subject: [PATCH] Add task to send canary to cbc proxy Create and schedule a Celery task that tests if we can send a canary message to cbc proxy. This will help us know if something happens to our connection to cbc proxy. Signed-off-by: Toby Lorne Co-authored-by: Pea Co-authored-by: Richard --- app/celery/scheduled_tasks.py | 10 +++++++++- app/config.py | 5 +++++ tests/app/celery/test_scheduled_tasks.py | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index e3fdb97f8..51618ae71 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -1,3 +1,5 @@ +import uuid + from datetime import ( datetime, timedelta @@ -8,7 +10,7 @@ from notifications_utils.statsd_decorators import statsd from sqlalchemy import and_ from sqlalchemy.exc import SQLAlchemyError -from app import notify_celery, zendesk_client +from app import cbc_proxy_client, notify_celery, zendesk_client from app.celery.tasks import ( process_job, get_recipient_csv_and_template_and_sender_id, @@ -291,3 +293,9 @@ def check_for_services_with_high_failure_rates_or_sending_to_tv_numbers(): message=message, ticket_type=zendesk_client.TYPE_INCIDENT ) + + +@notify_celery.task(name='send-canary-to-cbc-proxy') +def send_canary_to_cbc_proxy(): + identifier = str(uuid.uuid4()) + cbc_proxy_client.send_canary(identifier) diff --git a/app/config.py b/app/config.py index 06e561ccf..1ad9bcea7 100644 --- a/app/config.py +++ b/app/config.py @@ -303,6 +303,11 @@ class Config(object): 'schedule': crontab(hour=23, minute=00), 'options': {'queue': QueueNames.PERIODIC} }, + 'send-canary-to-cbc-proxy': { + 'task': 'send-canary-to-cbc-proxy', + 'schedule': timedelta(minutes=5), + 'options': {'queue': QueueNames.PERIODIC} + }, } CELERY_QUEUES = [] diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index 93be2a859..2c6bf3750 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -1,3 +1,4 @@ +import uuid from datetime import datetime, timedelta from unittest.mock import call @@ -555,3 +556,22 @@ def test_check_for_services_with_high_failure_rates_or_sending_to_tv_numbers( subject="[test] High failure rates for sms spotted for services", ticket_type='incident' ) + + +def test_send_canary_to_cbc_proxy_invokes_cbc_proxy_client( + mocker, +): + mock_send_canary = mocker.patch( + 'app.cbc_proxy_client.send_canary', + ) + + scheduled_tasks.send_canary_to_cbc_proxy() + + mock_send_canary.assert_called + # the 0th argument of the call to send_canary + identifier = mock_send_canary.mock_calls[0][1][0] + + try: + uuid.UUID(identifier) + except BaseException: + pytest.fail(f"{identifier} is not a valid uuid")