Esempio n. 1
0
    def iter(self, result, exc_info, start_time):  # noqa
        fut = Future(self.statistics['attempt_number'])
        if result is not NO_RESULT:
            trial_end_time = _utils.now()
            fut.set_result(result)
            retry = self.retry(fut)
        elif exc_info:
            trial_end_time = _utils.now()
            t, e, tb = exc_info
            _utils.capture(fut, exc_info)
            if isinstance(e, TryAgain):
                retry = True
            else:
                retry = self.retry(fut)
        else:
            if self.before is not None:
                self.before(self.fn, self.statistics['attempt_number'])

            return DoAttempt()

        if not retry:
            return fut.result()

        if self.after is not None:
            trial_time_taken = trial_end_time - start_time
            self.after(self.fn, self.statistics['attempt_number'],
                       trial_time_taken)

        delay_since_first_attempt = (_utils.now() -
                                     self.statistics['start_time'])
        self.statistics['delay_since_first_attempt'] = \
            delay_since_first_attempt
        if self.stop(self.statistics['attempt_number'],
                     delay_since_first_attempt):
            if self.retry_error_callback:
                return self.retry_error_callback(fut)
            retry_exc = self.retry_error_cls(fut)
            if self.reraise:
                raise retry_exc.reraise()
            six.raise_from(retry_exc, fut.exception())

        if self.wait:
            if self._wait_takes_result:
                sleep = self.wait(self.statistics['attempt_number'],
                                  delay_since_first_attempt,
                                  last_result=fut)
            else:
                sleep = self.wait(self.statistics['attempt_number'],
                                  delay_since_first_attempt)
        else:
            sleep = 0
        self.statistics['idle_for'] += sleep
        self.statistics['attempt_number'] += 1

        if self.before_sleep is not None:
            self.before_sleep(self, sleep=sleep, last_result=fut)

        return DoSleep(sleep)
Esempio n. 2
0
    def call(self, fn, *args, **kwargs):
        self.begin(fn)

        result = NO_RESULT
        exc_info = None
        start_time = _utils.now()

        while True:
            do = self.iter(result=result,
                           exc_info=exc_info,
                           start_time=start_time)
            if isinstance(do, DoAttempt):
                try:
                    result = yield fn(*args, **kwargs)
                    exc_info = None
                    continue
                except BaseException:
                    result = NO_RESULT
                    exc_info = sys.exc_info()
                    continue
            elif isinstance(do, DoSleep):
                result = NO_RESULT
                exc_info = None
                yield self.sleep(do)
            else:
                raise gen.Return(do)
Esempio n. 3
0
    def __init__(self, fn, args, kwargs):
        self.start_time = _utils.now()
        self.fn = fn
        self.args = args
        self.kwargs = kwargs

        self.idle_for = 0
        self.outcome = None
        self.outcome_timestamp = None
        self.attempt_number = 1
Esempio n. 4
0
    def __init__(self, retry_object, fn, args, kwargs):
        #: Retry call start timestamp
        self.start_time = _utils.now()
        #: Retry manager object
        self.retry_object = retry_object
        #: Function wrapped by this retry call
        self.fn = fn
        #: Arguments of the function wrapped by this retry call
        self.args = args
        #: Keyword arguments of the function wrapped by this retry call
        self.kwargs = kwargs

        #: The number of the current attempt
        self.attempt_number = 1
        #: Last outcome (result or exception) produced by the function
        self.outcome = None
        #: Timestamp of the last outcome
        self.outcome_timestamp = None
        #: Time spent sleeping in retries
        self.idle_for = 0
        #: Next action as decided by the retry manager
        self.next_action = None
Esempio n. 5
0
    def __init__(self, retry_object, fn, args, kwargs):
        #: Retry call start timestamp
        self.start_time = _utils.now()
        #: Retry manager object
        self.retry_object = retry_object
        #: Function wrapped by this retry call
        self.fn = fn
        #: Arguments of the function wrapped by this retry call
        self.args = args
        #: Keyword arguments of the function wrapped by this retry call
        self.kwargs = kwargs

        #: The number of the current attempt
        self.attempt_number = 1
        #: Last outcome (result or exception) produced by the function
        self.outcome = None
        #: Timestamp of the last outcome
        self.outcome_timestamp = None
        #: Time spent sleeping in retries
        self.idle_for = 0
        #: Next action as decided by the retry manager
        self.next_action = None
Esempio n. 6
0
 def set_exception(self, exc_info):
     ts = _utils.now()
     fut = Future(self.attempt_number)
     _utils.capture(fut, exc_info)
     self.outcome, self.outcome_timestamp = fut, ts
Esempio n. 7
0
 def set_result(self, val):
     ts = _utils.now()
     fut = Future(self.attempt_number)
     fut.set_result(val)
     self.outcome, self.outcome_timestamp = fut, ts
Esempio n. 8
0
 def begin(self, fn):
     self.statistics.clear()
     self.statistics["start_time"] = _utils.now()
     self.statistics["attempt_number"] = 1
     self.statistics["idle_for"] = 0
     self.fn = fn
Esempio n. 9
0
 def begin(self, fn):
     self.statistics.clear()
     self.statistics['start_time'] = _utils.now()
     self.statistics['attempt_number'] = 1
     self.statistics['idle_for'] = 0
     self.fn = fn
Esempio n. 10
0
 def set_exception(self, exc_info):
     ts = _utils.now()
     fut = Future(self.attempt_number)
     _utils.capture(fut, exc_info)
     self.outcome, self.outcome_timestamp = fut, ts
Esempio n. 11
0
 def set_result(self, val):
     ts = _utils.now()
     fut = Future(self.attempt_number)
     fut.set_result(val)
     self.outcome, self.outcome_timestamp = fut, ts
Esempio n. 12
0
 def begin(self, fn):
     self.statistics.clear()
     self.statistics['start_time'] = _utils.now()
     self.statistics['attempt_number'] = 1
     self.statistics['idle_for'] = 0
     self.fn = fn