mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 17:37:37 -02:30
properly update .failed, .last_job_id, and last_job_host_summary
This commit is contained in:
@@ -503,12 +503,30 @@ class JobEvent(BasePlaybookEvent):
|
|||||||
host_stats[stat] = self.event_data.get(stat, {}).get(host, 0)
|
host_stats[stat] = self.event_data.get(stat, {}).get(host, 0)
|
||||||
except AttributeError: # in case event_data[stat] isn't a dict.
|
except AttributeError: # in case event_data[stat] isn't a dict.
|
||||||
pass
|
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
|
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())
|
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
|
@property
|
||||||
def job_verbosity(self):
|
def job_verbosity(self):
|
||||||
return self.job.verbosity
|
return self.job.verbosity
|
||||||
|
|||||||
@@ -1129,20 +1129,6 @@ class JobHostSummary(CreatedModifiedModel):
|
|||||||
self.failed = bool(self.dark or self.failures)
|
self.failed = bool(self.dark or self.failures)
|
||||||
update_fields.append('failed')
|
update_fields.append('failed')
|
||||||
super(JobHostSummary, self).save(*args, **kwargs)
|
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):
|
class SystemJobOptions(BaseModel):
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ def test_parent_failed(emit, event):
|
|||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_host_summary_generation():
|
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 = Inventory()
|
||||||
inv.save()
|
inv.save()
|
||||||
Host.objects.bulk_create([
|
Host.objects.bulk_create([
|
||||||
@@ -108,6 +108,10 @@ def test_host_summary_generation():
|
|||||||
assert s.rescued == 0
|
assert s.rescued == 0
|
||||||
assert s.skipped == 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
|
@pytest.mark.django_db
|
||||||
def test_host_summary_generation_with_deleted_hosts():
|
def test_host_summary_generation_with_deleted_hosts():
|
||||||
|
|||||||
Reference in New Issue
Block a user