From 04a27d5b4d97e3d586f6706bd635a364f57a4b3e Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Tue, 13 Mar 2018 15:46:35 -0400 Subject: [PATCH] Namechange events_processed -> event_processing_finished from PR review, also adding tests to assert that the value is passed from the stdout_handle to the UnifiedJob object on finalization of job run in tasks.py --- awx/api/serializers.py | 10 +++++----- awx/api/views.py | 2 +- awx/main/models/unified_jobs.py | 2 +- awx/main/tests/unit/test_tasks.py | 11 ++++++++++- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 4bb51b6bb0..388d06d4a5 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -685,7 +685,7 @@ class UnifiedJobTemplateSerializer(BaseSerializer): class UnifiedJobSerializer(BaseSerializer): show_capabilities = ['start', 'delete'] - events_processed = serializers.BooleanField( + event_processing_finished = serializers.BooleanField( help_text=_('Indicates whether all of the events generated by this ' 'unified job have been saved to the database.'), read_only=True @@ -696,7 +696,7 @@ class UnifiedJobSerializer(BaseSerializer): fields = ('*', 'unified_job_template', 'launch_type', 'status', 'failed', 'started', 'finished', 'elapsed', 'job_args', 'job_cwd', 'job_env', 'job_explanation', 'execution_node', - 'result_traceback', 'events_processed') + 'result_traceback', 'event_processing_finished') extra_kwargs = { 'unified_job_template': { 'source': 'unified_job_template_id', @@ -786,13 +786,13 @@ class UnifiedJobSerializer(BaseSerializer): class UnifiedJobListSerializer(UnifiedJobSerializer): class Meta: - fields = ('*', '-job_args', '-job_cwd', '-job_env', '-result_traceback', '-events_processed') + fields = ('*', '-job_args', '-job_cwd', '-job_env', '-result_traceback', '-event_processing_finished') def get_field_names(self, declared_fields, info): field_names = super(UnifiedJobListSerializer, self).get_field_names(declared_fields, info) # Meta multiple inheritance and -field_name options don't seem to be # taking effect above, so remove the undesired fields here. - return tuple(x for x in field_names if x not in ('job_args', 'job_cwd', 'job_env', 'result_traceback', 'events_processed')) + return tuple(x for x in field_names if x not in ('job_args', 'job_cwd', 'job_env', 'result_traceback', 'event_processing_finished')) def get_types(self): if type(self) is UnifiedJobListSerializer: @@ -3509,7 +3509,7 @@ class WorkflowJobSerializer(LabelsListMixin, UnifiedJobSerializer): class Meta: model = WorkflowJob fields = ('*', 'workflow_job_template', 'extra_vars', 'allow_simultaneous', - '-execution_node', '-events_processed',) + '-execution_node', '-event_processing_finished',) def get_related(self, obj): res = super(WorkflowJobSerializer, self).get_related(obj) diff --git a/awx/api/views.py b/awx/api/views.py index 9d60056b58..e6d6e6074d 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -145,7 +145,7 @@ class UnifiedJobDeletionMixin(object): # Still allow deletion of new status, because these can be manually created if obj.status in ACTIVE_STATES and obj.status != 'new': raise PermissionDenied(detail=_("Cannot delete running job resource.")) - elif not obj.events_processed: + elif not obj.event_processing_finished: # Prohibit deletion if job events are still coming in if obj.finished and now() < obj.finished + dateutil.relativedelta.relativedelta(minutes=1): # less than 1 minute has passed since job finished and events are not in diff --git a/awx/main/models/unified_jobs.py b/awx/main/models/unified_jobs.py index d17d392434..fca04dc8b0 100644 --- a/awx/main/models/unified_jobs.py +++ b/awx/main/models/unified_jobs.py @@ -924,7 +924,7 @@ class UnifiedJob(PolymorphicModel, PasswordFieldsModel, CommonModelNameNotUnique return self.event_class.objects.filter(**{self.event_parent_key: self.id}) @property - def events_processed(self): + def event_processing_finished(self): ''' Returns True / False, whether all events from job have been saved ''' diff --git a/awx/main/tests/unit/test_tasks.py b/awx/main/tests/unit/test_tasks.py index 6265098c20..6def112522 100644 --- a/awx/main/tests/unit/test_tasks.py +++ b/awx/main/tests/unit/test_tasks.py @@ -35,7 +35,7 @@ from awx.main.models import ( from awx.main import tasks from awx.main.queue import CallbackQueueDispatcher -from awx.main.utils import encrypt_field, encrypt_value +from awx.main.utils import encrypt_field, encrypt_value, OutputEventFilter @@ -305,6 +305,15 @@ class TestGenericRun(TestJobExecution): ]: assert c in self.task.update_model.call_args_list + def test_event_count(self): + with mock.patch.object(self.task, 'get_stdout_handle') as mock_stdout: + handle = OutputEventFilter(lambda event_data: None) + handle._event_ct = 334 + mock_stdout.return_value = handle + self.task.run(self.pk) + + assert self.task.update_model.call_args[-1]['emitted_events'] == 334 + def test_artifact_cleanup(self): path = tempfile.NamedTemporaryFile(delete=False).name try: