From 917c6b405e97ed2371d32b6d96206ced224214c8 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Tue, 12 May 2020 14:46:57 -0400 Subject: [PATCH] properly update .failed, .last_job_id, and last_job_host_summary --- awx/main/models/events.py | 20 ++++++++++++++++++- awx/main/models/jobs.py | 14 ------------- .../tests/functional/models/test_events.py | 6 +++++- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/awx/main/models/events.py b/awx/main/models/events.py index 57de629a8f..e8d87253eb 100644 --- a/awx/main/models/events.py +++ b/awx/main/models/events.py @@ -503,12 +503,30 @@ class JobEvent(BasePlaybookEvent): host_stats[stat] = self.event_data.get(stat, {}).get(host, 0) except AttributeError: # in case event_data[stat] isn't a dict. pass - summaries[(host_id, host)] = JobHostSummary( + summary = JobHostSummary( created=now(), modified=now(), job_id=job.id, host_id=host_id, host_name=host, **host_stats ) + summary.failed = bool(summary.dark or summary.failures) + summaries[(host_id, host)] = summary JobHostSummary.objects.bulk_create(summaries.values()) + # update the last_job_id and last_job_host_summary_id + # in single queries + host_mapping = dict( + (summary['host'], summary['id']) + for summary in JobHostSummary.objects.filter(job_id=job.id).values('id', 'host') + ) + all_hosts = Host.objects.filter( + pk__in=host_mapping.keys() + ).only('id') + for h in all_hosts: + h.last_job_id = job.id + if h.id in host_mapping: + h.last_job_host_summary_id = host_mapping[h.id] + Host.objects.bulk_update(all_hosts, ['last_job_id', 'last_job_host_summary_id']) + + @property def job_verbosity(self): return self.job.verbosity diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 1ecb788900..8f42b9d577 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -1129,20 +1129,6 @@ class JobHostSummary(CreatedModifiedModel): self.failed = bool(self.dark or self.failures) update_fields.append('failed') super(JobHostSummary, self).save(*args, **kwargs) - self.update_host_last_job_summary() - - def update_host_last_job_summary(self): - update_fields = [] - if self.host is None: - return - if self.host.last_job_id != self.job_id: - self.host.last_job_id = self.job_id - update_fields.append('last_job_id') - if self.host.last_job_host_summary_id != self.id: - self.host.last_job_host_summary_id = self.id - update_fields.append('last_job_host_summary_id') - if update_fields: - self.host.save(update_fields=update_fields) class SystemJobOptions(BaseModel): diff --git a/awx/main/tests/functional/models/test_events.py b/awx/main/tests/functional/models/test_events.py index 6dd0cac06a..7f881a2fea 100644 --- a/awx/main/tests/functional/models/test_events.py +++ b/awx/main/tests/functional/models/test_events.py @@ -67,7 +67,7 @@ def test_parent_failed(emit, event): @pytest.mark.django_db def test_host_summary_generation(): - hostnames = [f'Host {i}' for i in range(500)] + hostnames = [f'Host {i}' for i in range(100)] inv = Inventory() inv.save() Host.objects.bulk_create([ @@ -108,6 +108,10 @@ def test_host_summary_generation(): assert s.rescued == 0 assert s.skipped == 0 + for host in Host.objects.all(): + assert host.last_job_id == j.id + assert host.last_job_host_summary.host == host + @pytest.mark.django_db def test_host_summary_generation_with_deleted_hosts():