From 47f5c17b56a266ae94d7fa838a99ccada80a6a7c Mon Sep 17 00:00:00 2001 From: chris meyers Date: Fri, 20 Mar 2020 08:27:22 -0400 Subject: [PATCH] log when notifications fail to send * If a job does not finish in the 5 second timeout. Let the user know that we failed to even try to send the notification. --- awx/main/tasks.py | 4 +++- awx/main/tests/unit/test_tasks.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index a66cdb7293..31423818b8 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -609,7 +609,7 @@ def handle_success_and_failure_notifications(job_id): while retries < 5: if uj.finished: uj.send_notification_templates('succeeded' if uj.status == 'successful' else 'failed') - break + return else: # wait a few seconds to avoid a race where the # events are persisted _before_ the UJ.status @@ -618,6 +618,8 @@ def handle_success_and_failure_notifications(job_id): time.sleep(1) uj = UnifiedJob.objects.get(pk=job_id) + logger.warn(f"Failed to even try to send notifications for job '{uj}' due to job not being in finished state.") + @task(queue=get_local_queuename) def update_inventory_computed_fields(inventory_id): diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index b520c00666..f676fd02ef 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -2381,3 +2381,23 @@ def test_managed_injector_redaction(injector_cls): if secret_field_name in template: env[env_name] = 'very_secret_value' assert 'very_secret_value' not in str(build_safe_env(env)) + + +@mock.patch('logging.getLogger') +def test_notification_job_not_finished(logging_getLogger, mocker): + uj = mocker.MagicMock() + uj.finished = False + logger = mocker.Mock() + logging_getLogger.return_value = logger + + with mocker.patch('awx.main.models.UnifiedJob.objects.get', uj): + tasks.handle_success_and_failure_notifications(1) + assert logger.warn.called_with(f"Failed to even try to send notifications for job '{uj}' due to job not being in finished state.") + + +def test_notification_job_finished(mocker): + uj = mocker.MagicMock(send_notification_templates=mocker.MagicMock(), finished=True) + + with mocker.patch('awx.main.models.UnifiedJob.objects.get', mocker.MagicMock(return_value=uj)): + tasks.handle_success_and_failure_notifications(1) + uj.send_notification_templates.assert_called()