diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index a7c1c6041d..2491ac6e9b 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -6,6 +6,7 @@ import hmac import json import yaml import logging +import time from urlparse import urljoin # Django @@ -680,9 +681,17 @@ class Job(UnifiedJob, JobOptions): dependencies.append(source.create_inventory_update(launch_type='dependency')) return dependencies - def notification_data(self): + def notification_data(self, block=5): data = super(Job, self).notification_data() all_hosts = {} + # NOTE: Probably related to job event slowness, remove at some point -matburt + if block: + summaries = self.job_host_summaries.all() + while block > 0 and not len(summaries): + time.sleep(1) + block -= 1 + else: + summaries = self.job_host_summaries.all() for h in self.job_host_summaries.all(): all_hosts[h.host_name] = dict(failed=h.failed, changed=h.changed, diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 17268515b5..877ed4b2d2 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -214,15 +214,18 @@ def handle_work_success(self, result, task_actual): friendly_name = "System Job" else: return - notification_body = instance.notification_data() - notification_subject = "{} #{} '{}' succeeded on Ansible Tower: {}".format(friendly_name, - task_actual['id'], - smart_str(instance_name), - notification_body['url']) - notification_body['friendly_name'] = friendly_name - send_notifications.delay([n.generate_notification(notification_subject, notification_body).id - for n in set(notification_templates.get('success', []) + notification_templates.get('any', []))], - job_id=task_actual['id']) + + all_notification_templates = set(notification_templates.get('success', []) + notification_templates.get('any', [])) + if len(all_notification_templates): + notification_body = instance.notification_data() + notification_subject = "{} #{} '{}' succeeded on Ansible Tower: {}".format(friendly_name, + task_actual['id'], + smart_str(instance_name), + notification_body['url']) + notification_body['friendly_name'] = friendly_name + send_notifications.delay([n.generate_notification(notification_subject, notification_body).id + for n in all_notification_templates], + job_id=task_actual['id']) @task(bind=True) def handle_work_error(self, task_id, subtasks=None): @@ -277,15 +280,18 @@ def handle_work_error(self, task_id, subtasks=None): (first_task_type, first_task_name, first_task_id) instance.save() instance.socketio_emit_status("failed") - notification_body = first_task.notification_data() - notification_subject = "{} #{} '{}' failed on Ansible Tower: {}".format(first_task_friendly_name, - first_task_id, - smart_str(first_task_name), - notification_body['url']) - notification_body['friendly_name'] = first_task_friendly_name - send_notifications.delay([n.generate_notification(notification_subject, notification_body).id - for n in set(notification_templates.get('error', []) + notification_templates.get('any', []))], - job_id=first_task_id) + + all_notification_templates = set(notification_templates.get('error', []) + notification_templates.get('any', [])) + if len(all_notification_templates): + notification_body = first_task.notification_data() + notification_subject = "{} #{} '{}' failed on Ansible Tower: {}".format(first_task_friendly_name, + first_task_id, + smart_str(first_task_name), + notification_body['url']) + notification_body['friendly_name'] = first_task_friendly_name + send_notifications.delay([n.generate_notification(notification_subject, notification_body).id + for n in all_notification_templates], + job_id=first_task_id) @task()