Python tenacity.retry_if_result() Examples
The following are 11
code examples of tenacity.retry_if_result().
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: driver.py From zun with Apache License 2.0 | 6 votes |
def _wait_for_init_container(self, context, container, timeout=3600): def retry_if_result_is_false(result): return result is False def check_init_container_stopped(): status = self.show(context, container).status if status == consts.STOPPED: return True elif status == consts.RUNNING: return False else: raise exception.ZunException( _("Container has unexpected status: %s") % status) r = tenacity.Retrying( stop=tenacity.stop_after_delay(timeout), wait=tenacity.wait_exponential(), retry=tenacity.retry_if_result(retry_if_result_is_false)) r.call(check_init_container_stopped)
Example #2
Source File: driver.py From zun with Apache License 2.0 | 6 votes |
def _wait_for_init_container(self, context, container, timeout=3600): def retry_if_result_is_false(result): return result is False def check_init_container_stopped(): status = self._show_container(context, container).status if status == consts.STOPPED: return True elif status == consts.RUNNING: return False else: raise exception.ZunException( _("Container has unexpected status: %s") % status) r = tenacity.Retrying( stop=tenacity.stop_after_delay(timeout), wait=tenacity.wait_exponential(), retry=tenacity.retry_if_result(retry_if_result_is_false)) r.call(check_init_container_stopped)
Example #3
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 6 votes |
def test_wait_chain_multiple_invocations(self): sleep_intervals = [] r = Retrying( sleep=sleep_intervals.append, wait=tenacity.wait_chain(*[ tenacity.wait_fixed(i + 1) for i in six.moves.range(3) ]), stop=tenacity.stop_after_attempt(5), retry=tenacity.retry_if_result(lambda x: x == 1), ) @r.wraps def always_return_1(): return 1 self.assertRaises(tenacity.RetryError, always_return_1) self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0]) sleep_intervals[:] = [] # Clear and restart retrying. self.assertRaises(tenacity.RetryError, always_return_1) self.assertEqual(sleep_intervals, [1.0, 2.0, 3.0, 3.0]) sleep_intervals[:] = []
Example #4
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 6 votes |
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 #5
Source File: s3.py From gnocchi with Apache License 2.0 | 5 votes |
def _put_object_safe(self, Bucket, Key, Body): put = self.s3.put_object(Bucket=Bucket, Key=Key, Body=Body) if self._consistency_stop: def _head(): return self.s3.head_object(Bucket=Bucket, Key=Key, IfMatch=put['ETag']) tenacity.Retrying( retry=tenacity.retry_if_result( lambda r: r['ETag'] != put['ETag']), wait=self._consistency_wait, stop=self._consistency_stop)(_head)
Example #6
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_retry_if_result(self): retry = (tenacity.retry_if_result(lambda x: x == 1)) def r(fut): retry_state = make_retry_state(1, 1.0, last_result=fut) return retry(retry_state) self.assertTrue(r(tenacity.Future.construct(1, 1, False))) self.assertFalse(r(tenacity.Future.construct(1, 2, False)))
Example #7
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_retry_any(self): retry = tenacity.retry_any( tenacity.retry_if_result(lambda x: x == 1), tenacity.retry_if_result(lambda x: x == 2)) def r(fut): retry_state = make_retry_state(1, 1.0, last_result=fut) return retry(retry_state) self.assertTrue(r(tenacity.Future.construct(1, 1, False))) self.assertTrue(r(tenacity.Future.construct(1, 2, False))) self.assertFalse(r(tenacity.Future.construct(1, 3, False))) self.assertFalse(r(tenacity.Future.construct(1, 1, True)))
Example #8
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_retry_all(self): retry = tenacity.retry_all( tenacity.retry_if_result(lambda x: x == 1), tenacity.retry_if_result(lambda x: isinstance(x, int))) def r(fut): retry_state = make_retry_state(1, 1.0, last_result=fut) return retry(retry_state) self.assertTrue(r(tenacity.Future.construct(1, 1, False))) self.assertFalse(r(tenacity.Future.construct(1, 2, False))) self.assertFalse(r(tenacity.Future.construct(1, 3, False))) self.assertFalse(r(tenacity.Future.construct(1, 1, True)))
Example #9
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 5 votes |
def test_retry_or(self): retry = (tenacity.retry_if_result(lambda x: x == "foo") | tenacity.retry_if_result(lambda x: isinstance(x, int))) def r(fut): retry_state = make_retry_state(1, 1.0, last_result=fut) return retry(retry_state) self.assertTrue(r(tenacity.Future.construct(1, "foo", False))) self.assertFalse(r(tenacity.Future.construct(1, "foobar", False))) self.assertFalse(r(tenacity.Future.construct(1, 2.2, False))) self.assertFalse(r(tenacity.Future.construct(1, 42, True)))
Example #10
Source File: health_manager.py From senlin with Apache License 2.0 | 4 votes |
def run_health_check(self, ctx, node): """Routine to check a node status from a url and recovery if necessary :param node: The node to be checked. :returns: True if node is healthy. False otherwise. """ max_unhealthy_retry = self.params['poll_url_retry_limit'] retry_interval = self.params['poll_url_retry_interval'] def _return_last_value(retry_state): return retry_state.outcome.result() @tenacity.retry( retry=tenacity.retry_if_result(lambda x: x is False), wait=tenacity.wait_fixed(retry_interval), retry_error_callback=_return_last_value, stop=tenacity.stop_after_attempt(max_unhealthy_retry) ) def _poll_url_with_retry(url): return self._poll_url(url, node) try: if node.status != consts.NS_ACTIVE: LOG.info("%s for %s: node is not in ACTIVE state, so skip " "poll url", consts.POLL_URL_PASS, node.name) return True url_template = self.params['poll_url'] url = self._expand_url_template(url_template, node) # If health check returns True, return True to mark node as # healthy. Else return True to mark node as healthy if we are still # within the node's grace period to allow the node to warm-up. # Return False to mark the node as unhealthy if we are outside the # grace period. return (_poll_url_with_retry(url) or self._node_within_grace_period(node)) except Exception as ex: LOG.warning( "%s for %s: Ignoring error on poll URL: %s", consts.POLL_URL_PASS, node.name, ex ) # treat node as healthy when an exception is encountered return True
Example #11
Source File: test_tenacity.py From tenacity with Apache License 2.0 | 4 votes |
def test_wait_retry_state_attributes(self): class ExtractCallState(Exception): pass # retry_state is mutable, so return it as an exception to extract the # exact values it has when wait is called and bypass any other logic. def waitfunc(retry_state): raise ExtractCallState(retry_state) retrying = Retrying( wait=waitfunc, retry=(tenacity.retry_if_exception_type() | tenacity.retry_if_result(lambda result: result == 123))) def returnval(): return 123 try: retrying.call(returnval) except ExtractCallState as err: retry_state = err.args[0] self.assertIs(retry_state.fn, returnval) self.assertEqual(retry_state.args, ()) self.assertEqual(retry_state.kwargs, {}) self.assertEqual(retry_state.outcome.result(), 123) self.assertEqual(retry_state.attempt_number, 1) self.assertGreater(retry_state.outcome_timestamp, retry_state.start_time) def dying(): raise Exception("Broken") try: retrying.call(dying) except ExtractCallState as err: retry_state = err.args[0] self.assertIs(retry_state.fn, dying) self.assertEqual(retry_state.args, ()) self.assertEqual(retry_state.kwargs, {}) self.assertEqual(str(retry_state.outcome.exception()), 'Broken') self.assertEqual(retry_state.attempt_number, 1) self.assertGreater(retry_state.outcome_timestamp, retry_state.start_time)