Python tenacity.stop_after_attempt() Examples

The following are 30 code examples of tenacity.stop_after_attempt(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tenacity , or try the search function .
Example #1
Source File: util.py    From PyAthena with MIT License 7 votes vote down vote up
def retry_api_call(func, config, logger=None, *args, **kwargs):
    retry = tenacity.Retrying(
        retry=retry_if_exception(
            lambda e: getattr(e, "response", {}).get("Error", {}).get("Code", None)
            in config.exceptions
            if e
            else False
        ),
        stop=stop_after_attempt(config.attempt),
        wait=wait_exponential(
            multiplier=config.multiplier,
            max=config.max_delay,
            exp_base=config.exponential_base,
        ),
        after=after_log(logger, logger.level) if logger else None,
        reraise=True,
    )
    return retry(func, *args, **kwargs) 
Example #2
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_wait_backward_compat_with_result(self):
        captures = []

        def wait_capture(attempt, delay, last_result=None):
            captures.append(last_result)
            return 1

        def dying():
            raise Exception("Broken")

        r_attempts = 10
        r = Retrying(wait=wait_capture, sleep=lambda secs: None,
                     stop=tenacity.stop_after_attempt(r_attempts),
                     reraise=True)
        with reports_deprecation_warning():
            self.assertRaises(Exception, r.call, dying)
        self.assertEqual(r_attempts - 1, len(captures))
        self.assertTrue(all([r.failed for r in captures])) 
Example #3
Source File: test_etcd3.py    From python-etcd3 with Apache License 2.0 6 votes vote down vote up
def etcd(self):
        endpoint = os.environ.get('PYTHON_ETCD_HTTP_URL')
        timeout = 5
        if endpoint:
            url = urlparse(endpoint)
            with etcd3.client(host=url.hostname,
                              port=url.port,
                              timeout=timeout) as client:
                yield client
        else:
            with etcd3.client() as client:
                yield client

        @retry(wait=wait_fixed(2), stop=stop_after_attempt(3))
        def delete_keys_definitely():
            # clean up after fixture goes out of scope
            etcdctl('del', '--prefix', '/')
            out = etcdctl('get', '--prefix', '/')
            assert 'kvs' not in out

        delete_keys_definitely() 
Example #4
Source File: __init__.py    From aodh with Apache License 2.0 6 votes vote down vote up
def get_connection_from_config(conf):
    retries = conf.database.max_retries
    url = conf.database.connection
    connection_scheme = urlparse.urlparse(url).scheme
    LOG.debug('looking for %(name)r driver in %(namespace)r',
              {'name': connection_scheme, 'namespace': _NAMESPACE})
    mgr = driver.DriverManager(_NAMESPACE, connection_scheme)

    @tenacity.retry(
        wait=tenacity.wait_fixed(conf.database.retry_interval),
        stop=tenacity.stop_after_attempt(retries if retries >= 0 else 5),
        reraise=True)
    def _get_connection():
        """Return an open connection to the database."""
        return mgr.driver(conf, url)

    return _get_connection() 
Example #5
Source File: shell.py    From vaultlocker with Apache License 2.0 6 votes vote down vote up
def _do_it_with_persistence(func, args, config):
    """Exec func with retries based on provided cli flags

    :param: func: function to attempt to execute
    :param: args: argparser generated cli arguments
    :param: config: configparser object of vaultlocker config
    """
    @tenacity.retry(
        wait=tenacity.wait_fixed(1),
        reraise=True,
        stop=(
            tenacity.stop_after_delay(args.retry) if args.retry > 0
            else tenacity.stop_after_attempt(1)
            ),
        retry=(
            tenacity.retry_if_exception(hvac.exceptions.VaultNotInitialized) |
            tenacity.retry_if_exception(hvac.exceptions.VaultDown)
            )
        )
    def _do_it():
        client = _vault_client(config)
        func(args, client, config)
    _do_it() 
Example #6
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_retry_error_callback(self):
        num_attempts = 3

        def retry_error_callback(retry_state):
            retry_error_callback.called_times += 1
            return retry_state.outcome

        retry_error_callback.called_times = 0

        @retry(stop=tenacity.stop_after_attempt(num_attempts),
               retry_error_callback=retry_error_callback)
        def _foobar():
            self._attempt_number += 1
            raise Exception("This exception should not be raised")

        result = _foobar()

        self.assertEqual(retry_error_callback.called_times, 1)
        self.assertEqual(num_attempts, self._attempt_number)
        self.assertIsInstance(result, tenacity.Future) 
Example #7
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_retry_error_callback_backward_compat(self):
        num_attempts = 3

        def retry_error_callback(fut):
            retry_error_callback.called_times += 1
            return fut

        retry_error_callback.called_times = 0

        @retry(stop=tenacity.stop_after_attempt(num_attempts),
               retry_error_callback=retry_error_callback)
        def _foobar():
            self._attempt_number += 1
            raise Exception("This exception should not be raised")

        with reports_deprecation_warning():
            result = _foobar()

        self.assertEqual(retry_error_callback.called_times, 1)
        self.assertEqual(num_attempts, self._attempt_number)
        self.assertIsInstance(result, tenacity.Future) 
Example #8
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_reraise_timeout_from_retry_error(self):
        calls = []

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(2),
               retry=lambda retry_state: True)
        def _mock_fn():
            calls.append('x')

        def _reraised_mock_fn():
            try:
                _mock_fn()
            except tenacity.RetryError as retry_err:
                retry_err.reraise()

        self.assertRaises(tenacity.RetryError, _reraised_mock_fn)
        self.assertEqual(2, len(calls)) 
Example #9
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_reraise_from_retry_error(self):
        calls = []

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(2))
        def _raise_key_error():
            calls.append('x')
            raise KeyError("Bad key")

        def _reraised_key_error():
            try:
                _raise_key_error()
            except tenacity.RetryError as retry_err:
                retry_err.reraise()

        self.assertRaises(KeyError, _reraised_key_error)
        self.assertEqual(2, len(calls)) 
Example #10
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_before_sleep_log_returns(self, exc_info=False):
        thing = NoneReturnUntilAfterCount(2)
        logger = logging.getLogger(self.id())
        logger.propagate = False
        logger.setLevel(logging.INFO)
        handler = CapturingHandler()
        logger.addHandler(handler)
        try:
            _before_sleep = tenacity.before_sleep_log(logger,
                                                      logging.INFO,
                                                      exc_info=exc_info)
            _retry = tenacity.retry_if_result(lambda result: result is None)
            retrying = Retrying(wait=tenacity.wait_fixed(0.01),
                                stop=tenacity.stop_after_attempt(3),
                                retry=_retry, before_sleep=_before_sleep)
            retrying.call(thing.go)
        finally:
            logger.removeHandler(handler)

        etalon_re = r'^Retrying .* in 0\.01 seconds as it returned None\.$'
        self.assertEqual(len(handler.records), 2)
        fmt = logging.Formatter().format
        self.assertRegexpMatches(fmt(handler.records[0]), etalon_re)
        self.assertRegexpMatches(fmt(handler.records[1]), etalon_re) 
Example #11
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_before_sleep_backward_compat(self):
        def _before_sleep(retry_obj, sleep, last_result):
            self.assertGreater(sleep, 0)
            _before_sleep.attempt_number = \
                retry_obj.statistics['attempt_number']
        _before_sleep.attempt_number = 0

        @retry(wait=tenacity.wait_fixed(0.01),
               stop=tenacity.stop_after_attempt(3),
               before_sleep=_before_sleep)
        def _test_before_sleep():
            if _before_sleep.attempt_number < 2:
                raise Exception("testing before_sleep_attempts handler")

        with reports_deprecation_warning():
            _test_before_sleep()
        self.assertEqual(_before_sleep.attempt_number, 2) 
Example #12
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_after_attempts(self):
        TestBeforeAfterAttempts._attempt_number = 0

        def _after(retry_state):
            TestBeforeAfterAttempts._attempt_number = \
                retry_state.attempt_number

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(3),
               after=_after)
        def _test_after():
            if TestBeforeAfterAttempts._attempt_number < 2:
                raise Exception("testing after_attempts handler")
            else:
                pass

        _test_after()

        self.assertTrue(TestBeforeAfterAttempts._attempt_number == 2) 
Example #13
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_before_attempts(self):
        TestBeforeAfterAttempts._attempt_number = 0

        def _before(retry_state):
            TestBeforeAfterAttempts._attempt_number = \
                retry_state.attempt_number

        @retry(wait=tenacity.wait_fixed(1),
               stop=tenacity.stop_after_attempt(1),
               before=_before)
        def _test_before():
            pass

        _test_before()

        self.assertTrue(TestBeforeAfterAttempts._attempt_number == 1) 
Example #14
Source File: test_tenacity.py    From tenacity with Apache License 2.0 6 votes vote down vote up
def test_retry_child_class_with_override_backward_compat(self):

        class MyStop(tenacity.stop_after_attempt):
            def __init__(self):
                super(MyStop, self).__init__(1)

            def __call__(self, attempt_number, seconds_since_start):
                return super(MyStop, self).__call__(
                    attempt_number, seconds_since_start)
        retrying = Retrying(wait=tenacity.wait_fixed(0.01),
                            stop=MyStop())

        def failing():
            raise NotImplementedError()
        with pytest.raises(RetryError):
            retrying.call(failing) 
Example #15
Source File: commit_classifier.py    From bugbug with Mozilla Public License 2.0 6 votes vote down vote up
def clone_git_repo(self, repo_url, repo_dir, rev="origin/branches/default/tip"):
        logger.info(f"Cloning {repo_url}...")

        if not os.path.exists(repo_dir):
            tenacity.retry(
                wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
                stop=tenacity.stop_after_attempt(5),
            )(
                lambda: subprocess.run(
                    ["git", "clone", "--quiet", repo_url, repo_dir], check=True
                )
            )()

        tenacity.retry(
            wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
            stop=tenacity.stop_after_attempt(5),
        )(
            lambda: subprocess.run(
                ["git", "fetch"], cwd=repo_dir, capture_output=True, check=True,
            )
        )()

        subprocess.run(
            ["git", "checkout", rev], cwd=repo_dir, capture_output=True, check=True
        ) 
Example #16
Source File: test_http.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_retry_on_conn_error(self, mocked_session):

        retry_args = dict(
            wait=tenacity.wait_none(),
            stop=tenacity.stop_after_attempt(7),
            retry=tenacity.retry_if_exception_type(
                requests.exceptions.ConnectionError
            )
        )

        def send_and_raise(unused_request, **kwargs):
            raise requests.exceptions.ConnectionError

        mocked_session().send.side_effect = send_and_raise
        # The job failed for some reason
        with self.assertRaises(tenacity.RetryError):
            self.get_hook.run_with_advanced_retry(
                endpoint='v1/test',
                _retry_args=retry_args
            )
        self.assertEqual(
            self.get_hook._retry_obj.stop.max_attempt_number + 1,
            mocked_session.call_count
        ) 
Example #17
Source File: test_http.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_run_with_advanced_retry(self, m):

        m.get(
            'http://test:8080/v1/test',
            status_code=200,
            reason='OK'
        )

        retry_args = dict(
            wait=tenacity.wait_none(),
            stop=tenacity.stop_after_attempt(3),
            retry=tenacity.retry_if_exception_type(Exception),
            reraise=True
        )
        with mock.patch(
            'airflow.hooks.base_hook.BaseHook.get_connection',
            side_effect=get_airflow_connection
        ):
            response = self.get_hook.run_with_advanced_retry(
                endpoint='v1/test',
                _retry_args=retry_args
            )
            self.assertIsInstance(response, requests.Response) 
Example #18
Source File: shippable_api.py    From ansibullbot with GNU General Public License v3.0 6 votes vote down vote up
def _fetch(self, url, verb='get', **kwargs):
        """return response or None in case of failure, try twice"""
        @retry(stop=stop_after_attempt(2), wait=wait_fixed(2))
        def _inner_fetch(verb='get'):
            headers = {
                'Authorization': 'apiToken %s' % C.DEFAULT_SHIPPABLE_TOKEN
            }

            logging.info(u'%s %s' % (verb, url))
            http_method = getattr(requests, verb)
            resp = http_method(url, headers=headers, **kwargs)
            logging.info(u'shippable status code: %s' % resp.status_code)
            logging.info(u'shippable reason: %s' % resp.reason)

            if resp.status_code not in [200, 302, 400]:
                logging.error(u'RC: %s', resp.status_code)
                raise TryAgain

            return resp

        try:
            logging.debug(u'%s' % url)
            return _inner_fetch(verb=verb)
        except RetryError as e:
            logging.error(e) 
Example #19
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_stats_failing(self):
        @retry(stop=tenacity.stop_after_attempt(2))
        def _foobar():
            raise ValueError(42)

        self.assertEqual({}, _foobar.retry.statistics)
        try:
            _foobar()
        except Exception:
            pass
        self.assertEqual(2, _foobar.retry.statistics['attempt_number']) 
Example #20
Source File: microannotate_generator.py    From bugbug with Mozilla Public License 2.0 5 votes vote down vote up
def clone_git_repo(self):
        tenacity.retry(
            wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
            stop=tenacity.stop_after_attempt(5),
        )(
            lambda: subprocess.run(
                ["git", "clone", "--quiet", self.repo_url, self.git_repo_path],
                check=True,
            )
        )()

        try:
            tenacity.retry(
                wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
                stop=tenacity.stop_after_attempt(5),
            )(
                lambda: subprocess.run(
                    ["git", "pull", "--quiet", self.repo_url, "master"],
                    cwd=self.git_repo_path,
                    capture_output=True,
                    check=True,
                )
            )()
        except subprocess.CalledProcessError as e:
            # When the repo is empty.
            if b"Couldn't find remote ref master" in e.stdout:
                pass 
Example #21
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_before_sleep(self):
        def _before_sleep(retry_state):
            self.assertGreater(retry_state.next_action.sleep, 0)
            _before_sleep.attempt_number = retry_state.attempt_number

        @retry(wait=tenacity.wait_fixed(0.01),
               stop=tenacity.stop_after_attempt(3),
               before_sleep=_before_sleep)
        def _test_before_sleep():
            if _before_sleep.attempt_number < 2:
                raise Exception("testing before_sleep_attempts handler")

        _test_before_sleep()
        self.assertEqual(_before_sleep.attempt_number, 2) 
Example #22
Source File: bugzilla.py    From bugbug with Mozilla Public License 2.0 5 votes vote down vote up
def download_bugs(bug_ids, products=None, security=False):
    old_bug_count = 0
    new_bug_ids = set(int(bug_id) for bug_id in bug_ids)
    for bug in get_bugs(include_invalid=True):
        old_bug_count += 1
        if int(bug["id"]) in new_bug_ids:
            new_bug_ids.remove(bug["id"])

    print(f"Loaded {old_bug_count} bugs.")

    new_bug_ids = sorted(list(new_bug_ids))

    CHUNK_SIZE = 100

    chunks = (
        new_bug_ids[i : (i + CHUNK_SIZE)]
        for i in range(0, len(new_bug_ids), CHUNK_SIZE)
    )

    @tenacity.retry(
        stop=tenacity.stop_after_attempt(7),
        wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
    )
    def get_chunk(chunk):
        new_bugs = get(chunk)

        if not security:
            new_bugs = [bug for bug in new_bugs.values() if len(bug["groups"]) == 0]

        if products is not None:
            new_bugs = [bug for bug in new_bugs.values() if bug["product"] in products]

        return new_bugs

    with tqdm(total=len(new_bug_ids)) as progress_bar:
        for chunk in chunks:
            new_bugs = get_chunk(chunk)

            progress_bar.update(len(chunk))

            db.append(BUGS_DB, new_bugs) 
Example #23
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_before_sleep_backward_compat_method(self):
        self.slept = 0

        @retry(wait=tenacity.wait_fixed(0.01),
               stop=tenacity.stop_after_attempt(3),
               before_sleep=self._before_sleep)
        def _test_before_sleep():
            raise Exception("testing before_sleep_attempts handler")

        try:
            _test_before_sleep()
        except tenacity.RetryError:
            pass

        self.assertEqual(self.slept, 2) 
Example #24
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_before_sleep_log_raises_with_exc_info(self):
        thing = NoIOErrorAfterCount(2)
        logger = logging.getLogger(self.id())
        logger.propagate = False
        logger.setLevel(logging.INFO)
        handler = CapturingHandler()
        logger.addHandler(handler)
        try:
            _before_sleep = tenacity.before_sleep_log(logger,
                                                      logging.INFO,
                                                      exc_info=True)
            retrying = Retrying(wait=tenacity.wait_fixed(0.01),
                                stop=tenacity.stop_after_attempt(3),
                                before_sleep=_before_sleep)
            retrying.call(thing.go)
        finally:
            logger.removeHandler(handler)

        etalon_re = re.compile(r"^Retrying .* in 0\.01 seconds as it raised "
                               r"(IO|OS)Error: Hi there, I'm an IOError\.{0}"
                               r"Traceback \(most recent call last\):{0}"
                               r".*$".format(os.linesep),
                               flags=re.MULTILINE)
        self.assertEqual(len(handler.records), 2)
        fmt = logging.Formatter().format
        self.assertRegexpMatches(fmt(handler.records[0]), etalon_re)
        self.assertRegexpMatches(fmt(handler.records[1]), etalon_re) 
Example #25
Source File: __init__.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def get_connection_from_config():
    retries = CONF.database.max_retries
    url = CONF.database.connection

    try:
        # TOTO(iafek): check why this call randomly fails
        connection_scheme = urlparse.urlparse(url).scheme
        LOG.debug('looking for %(name)r driver in %(namespace)r',
                  {'name': connection_scheme, 'namespace': _NAMESPACE})
        mgr = driver.DriverManager(_NAMESPACE, connection_scheme)

    except Exception:
        LOG.exception('Failed to get scheme %s.' % url)
        return None

    @tenacity.retry(
        wait=tenacity.wait_fixed(CONF.database.retry_interval),
        stop=tenacity.stop_after_attempt(retries),
        after=tenacity.after_log(LOG, log.WARN),
        reraise=True)
    def _get_connection():
        """Return an open connection to the database."""
        conn = mgr.driver(url)
        session = conn._engine_facade.get_session()
        session.execute('SELECT 1;')
        return conn

    return _get_connection() 
Example #26
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_reraise_by_default(self):
        calls = []

        @retry(wait=tenacity.wait_fixed(0.1),
               stop=tenacity.stop_after_attempt(2),
               reraise=True)
        def _reraised_by_default():
            calls.append('x')
            raise KeyError("Bad key")

        self.assertRaises(KeyError, _reraised_by_default)
        self.assertEqual(2, len(calls)) 
Example #27
Source File: regressor_finder.py    From bugbug with Mozilla Public License 2.0 5 votes vote down vote up
def clone_git_repo(self, repo_url, repo_dir):
        if not os.path.exists(repo_dir):
            tenacity.retry(
                wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
                stop=tenacity.stop_after_attempt(5),
            )(
                lambda: subprocess.run(
                    ["git", "clone", "--quiet", repo_url, repo_dir], check=True
                )
            )()

            logger.info(f"{repo_dir} cloned")

        logger.info(f"Fetching {repo_dir}")

        tenacity.retry(
            wait=tenacity.wait_exponential(multiplier=1, min=16, max=64),
            stop=tenacity.stop_after_attempt(5),
        )(
            lambda: subprocess.run(
                ["git", "fetch", "--quiet"],
                cwd=repo_dir,
                capture_output=True,
                check=True,
            )
        )()

        logger.info(f"{repo_dir} fetched") 
Example #28
Source File: http.py    From airflow with Apache License 2.0 5 votes vote down vote up
def run_with_advanced_retry(self, _retry_args, *args, **kwargs):
        """
        Runs Hook.run() with a Tenacity decorator attached to it. This is useful for
        connectors which might be disturbed by intermittent issues and should not
        instantly fail.

        :param _retry_args: Arguments which define the retry behaviour.
            See Tenacity documentation at https://github.com/jd/tenacity
        :type _retry_args: dict


        .. code-block:: python

            hook = HttpHook(http_conn_id='my_conn',method='GET')
            retry_args = dict(
                 wait=tenacity.wait_exponential(),
                 stop=tenacity.stop_after_attempt(10),
                 retry=requests.exceptions.ConnectionError
             )
             hook.run_with_advanced_retry(
                     endpoint='v1/test',
                     _retry_args=retry_args
                 )

        """
        self._retry_obj = tenacity.Retrying(
            **_retry_args
        )

        return self._retry_obj(self.run, *args, **kwargs) 
Example #29
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_context_manager_retry_error(self):
        from tenacity import Retrying

        retry = Retrying(stop=tenacity.stop_after_attempt(2))

        def test():
            for attempt in retry:
                with attempt:
                    raise Exception("Retry it!")

        self.assertRaises(RetryError, test) 
Example #30
Source File: test_tenacity.py    From tenacity with Apache License 2.0 5 votes vote down vote up
def test_context_manager_reraise(self):
        from tenacity import Retrying

        class CustomError(Exception):
            pass

        retry = Retrying(reraise=True, stop=tenacity.stop_after_attempt(2))

        def test():
            for attempt in retry:
                with attempt:
                    raise CustomError("Don't retry!")

        self.assertRaises(CustomError, test)