Best Python code snippet using autotest_python
monitor_db.py
Source:monitor_db.py  
...376                                                      is_active=False,377                                                      is_complete=False))378    def _recover_processes(self):379        agent_tasks = self._create_recovery_agent_tasks()380        self._register_pidfiles(agent_tasks)381        _drone_manager.refresh()382        self._recover_tasks(agent_tasks)383        self._recover_pending_entries()384        self._check_for_unrecovered_verifying_entries()385        self._reverify_remaining_hosts()386        # reinitialize drones after killing orphaned processes, since they can387        # leave around files when they die388        _drone_manager.execute_actions()389        _drone_manager.reinitialize_drones()390    def _create_recovery_agent_tasks(self):391        return (self._get_queue_entry_agent_tasks()392                + self._get_special_task_agent_tasks(is_active=True))393    def _get_queue_entry_agent_tasks(self):394        """395        Get agent tasks for all hqe in the specified states.396        Loosely this translates to taking a hqe in one of the specified states,397        say parsing, and getting an AgentTask for it, like the FinalReparseTask,398        through _get_agent_task_for_queue_entry. Each queue entry can only have399        one agent task at a time, but there might be multiple queue entries in400        the group.401        @return: A list of AgentTasks.402        """403        # host queue entry statuses handled directly by AgentTasks (Verifying is404        # handled through SpecialTasks, so is not listed here)405        statuses = (models.HostQueueEntry.Status.STARTING,406                    models.HostQueueEntry.Status.RUNNING,407                    models.HostQueueEntry.Status.GATHERING,408                    models.HostQueueEntry.Status.PARSING,409                    models.HostQueueEntry.Status.ARCHIVING)410        status_list = ','.join("'%s'" % status for status in statuses)411        queue_entries = scheduler_models.HostQueueEntry.fetch(412                where='status IN (%s)' % status_list)413        autotest_stats.Gauge('scheduler.jobs_per_tick').send(414                'running', len(queue_entries))415        agent_tasks = []416        used_queue_entries = set()417        for entry in queue_entries:418            if self.get_agents_for_entry(entry):419                # already being handled420                continue421            if entry in used_queue_entries:422                # already picked up by a synchronous job423                continue424            agent_task = self._get_agent_task_for_queue_entry(entry)425            agent_tasks.append(agent_task)426            used_queue_entries.update(agent_task.queue_entries)427        return agent_tasks428    def _get_special_task_agent_tasks(self, is_active=False):429        special_tasks = models.SpecialTask.objects.filter(430                is_active=is_active, is_complete=False)431        return [self._get_agent_task_for_special_task(task)432                for task in special_tasks]433    def _get_agent_task_for_queue_entry(self, queue_entry):434        """435        Construct an AgentTask instance for the given active HostQueueEntry.436        @param queue_entry: a HostQueueEntry437        @return: an AgentTask to run the queue entry438        """439        task_entries = queue_entry.job.get_group_entries(queue_entry)440        self._check_for_duplicate_host_entries(task_entries)441        if queue_entry.status in (models.HostQueueEntry.Status.STARTING,442                                  models.HostQueueEntry.Status.RUNNING):443            if queue_entry.is_hostless():444                return HostlessQueueTask(queue_entry=queue_entry)445            return QueueTask(queue_entries=task_entries)446        if queue_entry.status == models.HostQueueEntry.Status.GATHERING:447            return postjob_task.GatherLogsTask(queue_entries=task_entries)448        if queue_entry.status == models.HostQueueEntry.Status.PARSING:449            return postjob_task.FinalReparseTask(queue_entries=task_entries)450        if queue_entry.status == models.HostQueueEntry.Status.ARCHIVING:451            return postjob_task.ArchiveResultsTask(queue_entries=task_entries)452        raise scheduler_lib.SchedulerError(453                '_get_agent_task_for_queue_entry got entry with '454                'invalid status %s: %s' % (queue_entry.status, queue_entry))455    def _check_for_duplicate_host_entries(self, task_entries):456        non_host_statuses = (models.HostQueueEntry.Status.PARSING,457                             models.HostQueueEntry.Status.ARCHIVING)458        for task_entry in task_entries:459            using_host = (task_entry.host is not None460                          and task_entry.status not in non_host_statuses)461            if using_host:462                self._assert_host_has_no_agent(task_entry)463    def _assert_host_has_no_agent(self, entry):464        """465        @param entry: a HostQueueEntry or a SpecialTask466        """467        if self.host_has_agent(entry.host):468            agent = tuple(self._host_agents.get(entry.host.id))[0]469            raise scheduler_lib.SchedulerError(470                    'While scheduling %s, host %s already has a host agent %s'471                    % (entry, entry.host, agent.task))472    def _get_agent_task_for_special_task(self, special_task):473        """474        Construct an AgentTask class to run the given SpecialTask and add it475        to this dispatcher.476        A special task is created through schedule_special_tasks, but only if477        the host doesn't already have an agent. This happens through478        add_agent_task. All special agent tasks are given a host on creation,479        and a Null hqe. To create a SpecialAgentTask object, you need a480        models.SpecialTask. If the SpecialTask used to create a SpecialAgentTask481        object contains a hqe it's passed on to the special agent task, which482        creates a HostQueueEntry and saves it as it's queue_entry.483        @param special_task: a models.SpecialTask instance484        @returns an AgentTask to run this SpecialTask485        """486        self._assert_host_has_no_agent(special_task)487        special_agent_task_classes = (prejob_task.CleanupTask,488                                      prejob_task.VerifyTask,489                                      prejob_task.RepairTask,490                                      prejob_task.ResetTask,491                                      prejob_task.ProvisionTask)492        for agent_task_class in special_agent_task_classes:493            if agent_task_class.TASK_TYPE == special_task.task:494                return agent_task_class(task=special_task)495        raise scheduler_lib.SchedulerError(496                'No AgentTask class for task', str(special_task))497    def _register_pidfiles(self, agent_tasks):498        for agent_task in agent_tasks:499            agent_task.register_necessary_pidfiles()500    def _recover_tasks(self, agent_tasks):501        orphans = _drone_manager.get_orphaned_autoserv_processes()502        for agent_task in agent_tasks:503            agent_task.recover()504            if agent_task.monitor and agent_task.monitor.has_process():505                orphans.discard(agent_task.monitor.get_process())506            self.add_agent_task(agent_task)507        self._check_for_remaining_orphan_processes(orphans)508    def _get_unassigned_entries(self, status):509        for entry in scheduler_models.HostQueueEntry.fetch(where="status = '%s'"510                                                           % status):511            if entry.status == status and not self.get_agents_for_entry(entry):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
