Repository: ansible/awx
File: awx/main/tasks/jobs.py (Python)

Code before:
        instance.log_lifecycle("post_run")

    def final_run_hook(self, instance, status, private_data_dir):
        """
        Hook for any steps to run after job/task is marked as complete.
        """
        instance.log_lifecycle("finalize_run")

        # Run task manager appropriately for speculative dependencies
        if instance.unifiedjob_blocked_jobs.exists():
            ScheduleTaskManager().schedule()
        if instance.spawned_by_workflow:
            ScheduleWorkflowManager().schedule()

    def should_use_fact_cache(self):
        return False

    @with_path_cleanup
    @with_signal_handling
    def run(self, pk, **kwargs):
        """
        Run the job/task and capture its output.
        """
        # Load the instance - status should be "running" from dispatch_waiting_jobs,
        # but may still be "waiting" if the worker picked this up before the status update landed.
        self.instance = self.update_model(pk)
        if self.instance.status == 'waiting':
            if self.instance.cancel_flag:
                UnifiedJob.objects.filter(pk=pk, status='waiting').update(status='canceled', start_args='')
                self.instance.status = 'canceled'
            else:
                UnifiedJob.objects.filter(pk=pk, status='waiting').update(status='running', start_args='')
                self.instance.status = 'running'
            self.instance.start_args = ''
        if self.instance.status != 'running':
            logger.error(f'Not starting {self.instance.status} task pk={pk} because its status "{self.instance.status}" is not expected')
            return

        self.instance.websocket_emit_status("running")
        status, rc = 'error', None
        self.runner_callback.event_ct = 0

        '''
        Needs to be an object property because status_handler uses it in a callback context
        '''

        self.safe_cred_env = {}
        private_data_dir = None

        try:
            if self.instance.execution_environment_id is None:

Review comment (suggestion):
I made it so the local project update will have the instance passed locally, and in that case `self.instance` will be non-null, so I would like to keep that non-null check, so we can do nothing in that case.

Code after:
        instance.log_lifecycle("post_run")

    def final_run_hook(self, instance, status, private_data_dir):
        """
        Hook for any steps to run after job/task is marked as complete.
        """
        instance.log_lifecycle("finalize_run")

        # Run task manager appropriately for speculative dependencies
        if instance.unifiedjob_blocked_jobs.exists():
            ScheduleTaskManager().schedule()
        if instance.spawned_by_workflow:
            ScheduleWorkflowManager().schedule()

    def should_use_fact_cache(self):
        return False

    @with_path_cleanup
    @with_signal_handling
    def run(self, pk, **kwargs):
        """
        Run the job/task and capture its output.
        """

        if not self.instance:  # Used to skip fetch for local runs
            # Load the instance
            self.instance = self.update_model(pk)

        # status should be "running" from dispatch_waiting_jobs,
        # but may still be "waiting" if the worker picked this up before the status update landed.
        if self.instance.status == 'waiting':
            UnifiedJob.objects.filter(pk=pk).update(status="running", start_args='')
            self.instance.refresh_from_db()

        if self.instance.status != 'running':
            logger.error(f'Not starting {self.instance.status} task pk={pk} because its status "{self.instance.status}" is not expected')
            return

        if self.instance.cancel_flag:
            self.instance = self.update_model(pk, status='canceled')
            self.instance.websocket_emit_status('canceled')
            return

        self.instance.websocket_emit_status("running")
        status, rc = 'error', None
        self.runner_callback.event_ct = 0

        '''
        Needs to be an object property because status_handler uses it in a callback context
        '''