diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 76a2895bf8..6bc85e3162 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -4,6 +4,7 @@ # Python from io import StringIO import datetime +import decimal import codecs import json import logging @@ -842,15 +843,16 @@ class UnifiedJob( if 'finished' not in update_fields: update_fields.append('finished') + dq = decimal.Decimal('1.000') + if self.elapsed is None: + self.elapsed = decimal.Decimal(0.0).quantize(dq) + # If we have a start and finished time, and haven't already calculated # out the time that elapsed, do so. - if self.started and self.finished and not self.elapsed: + if self.started and self.finished and self.elapsed == 0.0: td = self.finished - self.started - elapsed = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / (10 ** 6 * 1.0) - else: - elapsed = 0.0 - if self.elapsed != elapsed: - self.elapsed = str(elapsed) + elapsed = decimal.Decimal(td.total_seconds()) + self.elapsed = elapsed.quantize(dq) if 'elapsed' not in update_fields: update_fields.append('elapsed') diff --git a/awx_collection/test/awx/test_ad_hoc_wait.py b/awx_collection/test/awx/test_ad_hoc_wait.py index 943268184a..5718783015 100644 --- a/awx_collection/test/awx/test_ad_hoc_wait.py +++ b/awx_collection/test/awx/test_ad_hoc_wait.py @@ -13,9 +13,10 @@ def test_ad_hoc_command_wait_successful(run_module, admin_user): command = AdHocCommand.objects.create(status='successful', started=now(), finished=now()) result = run_module('tower_ad_hoc_command_wait', dict(command_id=command.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(command.finished)[:10] assert result.pop('started', '')[:10] == str(command.started)[:10] - assert result == {"status": "successful", "changed": False, "elapsed": str(command.elapsed), "id": command.id} + assert result == {"status": "successful", "changed": False, "elapsed": command.elapsed, "id": command.id} @pytest.mark.django_db @@ -23,13 +24,14 @@ def test_ad_hoc_command_wait_failed(run_module, admin_user): command = AdHocCommand.objects.create(status='failed', started=now(), finished=now()) result = run_module('tower_ad_hoc_command_wait', dict(command_id=command.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(command.finished)[:10] assert result.pop('started', '')[:10] == str(command.started)[:10] assert result == { "status": "failed", "failed": True, "changed": False, - "elapsed": str(command.elapsed), + "elapsed": command.elapsed, "id": command.id, "msg": "The ad hoc command - 1, failed", } diff --git a/awx_collection/test/awx/test_job.py b/awx_collection/test/awx/test_job.py index 012c1fd9d1..107d7c1616 100644 --- a/awx_collection/test/awx/test_job.py +++ b/awx_collection/test/awx/test_job.py @@ -13,9 +13,10 @@ def test_job_wait_successful(run_module, admin_user): job = Job.objects.create(status='successful', started=now(), finished=now()) result = run_module('tower_job_wait', dict(job_id=job.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(job.finished)[:10] assert result.pop('started', '')[:10] == str(job.started)[:10] - assert result == {"status": "successful", "changed": False, "elapsed": str(job.elapsed), "id": job.id} + assert result == {"status": "successful", "changed": False, "elapsed": job.elapsed, "id": job.id} @pytest.mark.django_db @@ -23,9 +24,10 @@ def test_job_wait_failed(run_module, admin_user): job = Job.objects.create(status='failed', started=now(), finished=now()) result = run_module('tower_job_wait', dict(job_id=job.id), admin_user) result.pop('invocation', None) + result['elapsed'] = float(result['elapsed']) assert result.pop('finished', '')[:10] == str(job.finished)[:10] assert result.pop('started', '')[:10] == str(job.started)[:10] - assert result == {"status": "failed", "failed": True, "changed": False, "elapsed": str(job.elapsed), "id": job.id, "msg": "Job with id 1 failed"} + assert result == {"status": "failed", "failed": True, "changed": False, "elapsed": job.elapsed, "id": job.id, "msg": "Job with id 1 failed"} @pytest.mark.django_db