当前位置: 首页>>代码示例>>Python>>正文


Python Handler.handle_finish方法代码示例

本文整理汇总了Python中handler.Handler.handle_finish方法的典型用法代码示例。如果您正苦于以下问题:Python Handler.handle_finish方法的具体用法?Python Handler.handle_finish怎么用?Python Handler.handle_finish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在handler.Handler的用法示例。


在下文中一共展示了Handler.handle_finish方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MultiJob

# 需要导入模块: from handler import Handler [as 别名]
# 或者: from handler.Handler import handle_finish [as 别名]
class MultiJob(object):

    def __init__(self):
        """
        MultiJob constructor

        """
        self._jobs = {}
        self.client = APIClient(opts=MASTER_OPTIONS)
        self.handler = Handler()

    def add(self, job):
        """
        Adds a job to be tracked. The job is published with the salt
        apiclient. The resulting dict containing the job id and the minions
        associated with the job id are stored for later use.

        @param salt_job - SaltCommand object containing a dictionary defining
            parameters of the salt job to be published
        @return - Boolean True for successful publish, Boolean False otherwise

        """
        pub_data = self.client.run(job.kwargs)
        job.set_pub_data(pub_data)
        self._jobs[job.jid] = job

    def is_finished(self):
        """
        Checks to see if all jobs are finished.

        @return - Boolean true for finished, Boolean false otherwise

        """
        return all([job.is_finished() for job in self._jobs.itervalues()])

    def should_process_event(self, event):
        """
        Checks whether or not we need to process an event.
        Events should have a jid and a return.
        The jid should be a job belonging to this MultiJob
        The job should not be finished yet.

        @param event - Dictionary representing an event.
        @return Boolean True for yes, False otherwise.

        """
        jid = event.get('jid')
        ret = event.get('return')
        if jid is None or ret is None:
            return False

        if jid not in self._jobs:
            return False

        job = self._jobs[jid]
        if job.is_finished():
            return False
        return True

    def wait(self, timeout):
        """
        Waits for all jobs so far to be finished. If a job finishes that is
        part of a sequence of jobs, the next job in the sequenced is
        published.

        @param timeout - Float or int describing number of seconds to wait
            in total before returning.
        @return dict - Dictionary of responses

        """
        start = time.time()
        timeout_at = start + timeout
        while True:

            # Break on timeout
            if time.time() > timeout_at:
                break

            # Listen for all events with tag set to ''.
            # Need to be able to listen for multiple jobs.
            event = self.client.get_event(tag='', wait=0.25)

            # Check for no event received
            if event is None:
                continue

            if self.should_process_event(event):
                job = self._jobs[event.get('jid')]
                job.add_minion_return(event)
                if job.is_finished():
                    self.handler.handle_finish(job)
                    if job.chain:
                        self.add(job.chain)

            # Break on all jobs finished
            if self.is_finished():
                break

        errors = []
        # Validate our jobs
#.........这里部分代码省略.........
开发者ID:dani4571,项目名称:cloud-workloads,代码行数:103,代码来源:job.py


注:本文中的handler.Handler.handle_finish方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。