diff --git a/awx/main/tasks.py b/awx/main/tasks.py index d60d936531..759c0f8a9d 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -43,6 +43,7 @@ from django.core.mail import send_mail from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ from django.core.cache import cache +from django.core.exceptions import ObjectDoesNotExist # AWX from awx.main.constants import CLOUD_PROVIDERS @@ -235,7 +236,11 @@ def _send_notification_templates(instance, status_str): @task(bind=True, queue='default') def handle_work_success(self, result, task_actual): - instance = UnifiedJob.get_instance_by_type(task_actual['type'], task_actual['id']) + try: + instance = UnifiedJob.get_instance_by_type(task_actual['type'], task_actual['id']) + except ObjectDoesNotExist: + logger.warning('Missing job `{}` in success callback.'.format(task_actual['id'])) + return if not instance: return diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 16b9bc6b14..204ef80352 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -2,10 +2,12 @@ from contextlib import contextmanager import pytest import yaml +import mock from awx.main.models import ( UnifiedJob, Notification, + ProjectUpdate ) from awx.main import tasks @@ -31,6 +33,13 @@ def test_send_notifications_job_id(mocker): assert UnifiedJob.objects.get.called_with(id=1) +def test_work_success_callback_missing_job(): + task_data = {'type': 'project_update', 'id': 9999} + with mock.patch('django.db.models.query.QuerySet.get') as get_mock: + get_mock.side_effect = ProjectUpdate.DoesNotExist() + assert tasks.handle_work_success(None, task_data) is None + + def test_send_notifications_list(mocker): patches = list()