From d72896f9a6ffe3d7372641bb4d2e33176193bc71 Mon Sep 17 00:00:00 2001 From: Jake Jackson Date: Wed, 20 Nov 2019 14:57:15 -0500 Subject: [PATCH 1/2] Added canceled_on field to unified_jobs model - When a job is canceled, the canceled_on field will populate with date/time --- awx/api/serializers.py | 8 ++++---- .../0101_v370_unifiedjob_canceled.py | 18 ++++++++++++++++++ awx/main/models/unified_jobs.py | 14 +++++++++++++- awx/main/tests/functional/api/test_job.py | 3 ++- 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 awx/main/migrations/0101_v370_unifiedjob_canceled.py diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 2598fd0e1e..a50065688f 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -117,7 +117,7 @@ SUMMARIZABLE_FK_FIELDS = { 'source_project': DEFAULT_SUMMARY_FIELDS + ('status', 'scm_type'), 'project_update': DEFAULT_SUMMARY_FIELDS + ('status', 'failed',), 'credential': DEFAULT_SUMMARY_FIELDS + ('kind', 'cloud', 'kubernetes', 'credential_type_id'), - 'job': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'elapsed', 'type'), + 'job': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'elapsed', 'type', 'canceled_on'), 'job_template': DEFAULT_SUMMARY_FIELDS, 'workflow_job_template': DEFAULT_SUMMARY_FIELDS, 'workflow_job': DEFAULT_SUMMARY_FIELDS, @@ -125,7 +125,7 @@ SUMMARIZABLE_FK_FIELDS = { 'workflow_approval': DEFAULT_SUMMARY_FIELDS + ('timeout',), 'schedule': DEFAULT_SUMMARY_FIELDS + ('next_run',), 'unified_job_template': DEFAULT_SUMMARY_FIELDS + ('unified_job_type',), - 'last_job': DEFAULT_SUMMARY_FIELDS + ('finished', 'status', 'failed', 'license_error'), + 'last_job': DEFAULT_SUMMARY_FIELDS + ('finished', 'status', 'failed', 'license_error', 'canceled_on'), 'last_job_host_summary': DEFAULT_SUMMARY_FIELDS + ('failed',), 'last_update': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'license_error'), 'current_update': DEFAULT_SUMMARY_FIELDS + ('status', 'failed', 'license_error'), @@ -719,7 +719,7 @@ class UnifiedJobSerializer(BaseSerializer): class Meta: model = UnifiedJob fields = ('*', 'unified_job_template', 'launch_type', 'status', - 'failed', 'started', 'finished', 'elapsed', 'job_args', + 'failed', 'started', 'finished', 'canceled_on', 'elapsed', 'job_args', 'job_cwd', 'job_env', 'job_explanation', 'execution_node', 'controller_node', 'result_traceback', 'event_processing_finished') @@ -2823,7 +2823,7 @@ class JobTemplateMixin(object): # .only('id', 'status', 'finished', 'polymorphic_ctype_id') optimized_qs = uj_qs.non_polymorphic() return [{ - 'id': x.id, 'status': x.status, 'finished': x.finished, + 'id': x.id, 'status': x.status, 'finished': x.finished, 'canceled_on': x.canceled_on, # Make type consistent with API top-level key, for instance workflow_job 'type': x.get_real_instance_class()._meta.verbose_name.replace(' ', '_') } for x in optimized_qs[:10]] diff --git a/awx/main/migrations/0101_v370_unifiedjob_canceled.py b/awx/main/migrations/0101_v370_unifiedjob_canceled.py new file mode 100644 index 0000000000..db7073c048 --- /dev/null +++ b/awx/main/migrations/0101_v370_unifiedjob_canceled.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.4 on 2019-11-25 20:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('main', '0100_v370_projectupdate_job_tags'), + ] + + operations = [ + migrations.AddField( + model_name='unifiedjob', + name='canceled_on', + field=models.DateTimeField(db_index=True, default=None, editable=False, help_text='The date and time when the cancel request was sent.', null=True), + ), + ] diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index 3613ac4d34..7658b997cd 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -630,6 +630,13 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique help_text=_("The date and time the job finished execution."), db_index=True, ) + canceled_on = models.DateTimeField( + null=True, + default=None, + editable=False, + help_text=_("The date and time when the cancel request was sent."), + db_index=True, + ) elapsed = models.DecimalField( max_digits=12, decimal_places=3, @@ -833,7 +840,12 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique self.unified_job_template = self._get_parent_instance() if 'unified_job_template' not in update_fields: update_fields.append('unified_job_template') - + + if self.cancel_flag and not self.canceled_on: + # Record the 'canceled' time. + self.canceled_on = now() + if 'canceled_on' not in update_fields: + update_fields.append('canceled_on') # Okay; we're done. Perform the actual save. result = super(UnifiedJob, self).save(*args, **kwargs) diff --git a/awx/main/tests/functional/api/test_job.py b/awx/main/tests/functional/api/test_job.py index 39c5d179a6..0692d70ffb 100644 --- a/awx/main/tests/functional/api/test_job.py +++ b/awx/main/tests/functional/api/test_job.py @@ -153,7 +153,8 @@ def test_summary_fields_recent_jobs(job_template, admin_user, get): 'id': job.id, 'status': 'failed', 'finished': job.finished, - 'type': 'job' + 'canceled_on': None, + 'type': 'job' } for job in jobs[-10:][::-1]] From 79b8e6b6f09846a328381d8d671d44d2b8e417fc Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Tue, 14 Jan 2020 16:00:33 -0500 Subject: [PATCH 2/2] renumber migrations correctly --- ..._unifiedjob_canceled.py => 0102_v370_unifiedjob_canceled.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename awx/main/migrations/{0101_v370_unifiedjob_canceled.py => 0102_v370_unifiedjob_canceled.py} (87%) diff --git a/awx/main/migrations/0101_v370_unifiedjob_canceled.py b/awx/main/migrations/0102_v370_unifiedjob_canceled.py similarity index 87% rename from awx/main/migrations/0101_v370_unifiedjob_canceled.py rename to awx/main/migrations/0102_v370_unifiedjob_canceled.py index db7073c048..b5909dd9bc 100644 --- a/awx/main/migrations/0101_v370_unifiedjob_canceled.py +++ b/awx/main/migrations/0102_v370_unifiedjob_canceled.py @@ -6,7 +6,7 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('main', '0100_v370_projectupdate_job_tags'), + ('main', '0101_v370_generate_new_uuids_for_iso_nodes'), ] operations = [