def handle_task(self, task, **kwargs):
        log_prefix = "%s - " % (task.id,)
        task_expiration = kwargs.get('task_expiration', None)
        if self.expire_task(task, task_expiration):
            return None
        # Used to add the command context to the task
        monitor = Monitor.registry[task.context['monitor']['name']]
        command = monitor.command
        task.context = command.build_context(task.context)
        previous_state = self.get_state(task.id)
        # check if the timeout is defined on the task first, if not then
        # go with what was passed into handle_task via run
        timeout = task.context.get('monitor_timeout',
                                   kwargs.get('monitor_timeout'))
        max_retries = task.context.get('max_retries',
                                       kwargs.get('max_retries'))
        last_attempt = int(task.attempt)
        current_attempt = last_attempt + 1
        result = self.execute_task(task, timeout, **kwargs)
        # Trying to emulate this:
        # http://nagios.sourceforge.net/docs/3_0/statetypes.html
        if result.state == types.STATE_OK:
            if (previous_state and not
                    previous_state.state == types.STATE_OK and
                    previous_state.state_type == types.STATE_TYPE_SOFT):
                result.state_type = types.STATE_TYPE_SOFT
        else:
            logger.debug(log_prefix + "current_attempt: %d, max_retries: %d",
                         current_attempt, max_retries)
            if current_attempt <= max_retries:
                # XXX Hate this logic - hope to find a cleaner way to handle
                #     it someday.
                if (not previous_state or
                   previous_state.state_type == types.STATE_TYPE_SOFT or
                   previous_state.state == types.STATE_OK):
                    result.state_type = types.STATE_TYPE_SOFT
                    delay = task.context.get('retry_delay',
                                             kwargs.get('retry_delay'))
                    delay = max(delay, 0)
                    logger.debug('Resubmitting task with %ds delay.', delay)
                    self.resubmit_task(task, delay, **kwargs)
            else:
                logger.debug("Retry limit hit, not resubmitting.")
        result.validate()
        return result
dataset/ETHPy150Open cloudtools/nymms/nymms/probe/Probe.py/Probe.handle_task/original