def run(self, tasklet, **kwds):
    """Run a tasklet with retry.

    The retry should be transparent to the caller: if no results
    are successful, the exception or result from the last retry is returned
    to the caller.

    Args:
      tasklet: the tasklet to run.
      **kwds: keywords arguments to run the tasklet.

    Raises:
      The exception from running the tasklet.

    Returns:
      The result from running the tasklet.
    """
    start_time = time.time()
    n = 1

    while True:
      e = None
      result = None
      got_result = False

      try:
        result = yield tasklet(**kwds)
        got_result = True
        if not self.should_retry(result):
          raise ndb.Return(result)
      except runtime.DeadlineExceededError:
        logging.debug(
            'Tasklet has exceeded request deadline after %s seconds total',
            time.time() - start_time)
        raise
      except self.retriable_exceptions, e:
        pass

      if n == 1:
        logging.debug('Tasklet is %r', tasklet)

      delay = self.retry_params.delay(n, start_time)

      if delay <= 0:
        logging.debug(
            'Tasklet failed after %s attempts and %s seconds in total',
            n, time.time() - start_time)
        if got_result:
          raise ndb.Return(result)
        elif e is not None:
          raise e
        else:
          assert False, 'Should never reach here.'

      if got_result:
        logging.debug(
            'Got result %r from tasklet.', result)
      else:
        logging.debug(
            'Got exception "%r" from tasklet.', e)
      logging.debug('Retry in %s seconds.', delay)
      n += 1
      yield tasklets.sleep(delay)
Exemple #2
0
    def run(self, tasklet, **kwds):
        """Run a tasklet with retry.

    The retry should be transparent to the caller: if no results
    are successful, the exception or result from the last retry is returned
    to the caller.

    Args:
      tasklet: the tasklet to run.
      **kwds: keywords arguments to run the tasklet.

    Raises:
      The exception from running the tasklet.

    Returns:
      The result from running the tasklet.
    """
        start_time = time.time()
        n = 1

        while True:
            e = None
            result = None
            got_result = False

            try:
                result = yield tasklet(**kwds)
                got_result = True
                if not self.should_retry(result):
                    raise ndb.Return(result)
            except runtime.DeadlineExceededError:
                logging.debug(
                    'Tasklet has exceeded request deadline after %s seconds total',
                    time.time() - start_time)
                raise
            except self.retriable_exceptions, e:
                pass

            if n == 1:
                logging.debug('Tasklet is %r', tasklet)

            delay = self.retry_params.delay(n, start_time)

            if delay <= 0:
                logging.debug(
                    'Tasklet failed after %s attempts and %s seconds in total',
                    n,
                    time.time() - start_time)
                if got_result:
                    raise ndb.Return(result)
                elif e is not None:
                    raise e
                else:
                    assert False, 'Should never reach here.'

            if got_result:
                logging.debug('Got result %r from tasklet.', result)
            else:
                logging.debug('Got exception "%r" from tasklet.', e)
            logging.debug('Retry in %s seconds.', delay)
            n += 1
            yield tasklets.sleep(delay)