Python backoff.on_predicate() Examples
The following are 26
code examples of backoff.on_predicate().
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
backoff
, or try the search function
.
Example #1
Source File: test_backoff_async.py From backoff with MIT License | 6 votes |
def test_on_exception_coro_cancelling(event_loop): sleep_started_event = asyncio.Event() @backoff.on_predicate(backoff.expo) async def coro(): sleep_started_event.set() try: await asyncio.sleep(10) except asyncio.CancelledError: return True return False task = event_loop.create_task(coro()) await sleep_started_event.wait() task.cancel() assert (await task)
Example #2
Source File: test_backoff_async.py From backoff with MIT License | 6 votes |
def test_on_predicate_on_regular_function_without_event_loop(monkeypatch): monkeypatch.setattr('time.sleep', lambda x: None) # Set default event loop to None. loop = asyncio.get_event_loop() asyncio.set_event_loop(None) try: @backoff.on_predicate(backoff.expo) def return_true(log, n): val = (len(log) == n - 1) log.append(val) return val log = [] ret = return_true(log, 3) assert ret is True assert 3 == len(log) finally: # Restore event loop. asyncio.set_event_loop(loop)
Example #3
Source File: test_backoff.py From backoff with MIT License | 5 votes |
def test_on_predicate(monkeypatch): monkeypatch.setattr('time.sleep', lambda x: None) @backoff.on_predicate(backoff.expo) def return_true(log, n): val = (len(log) == n - 1) log.append(val) return val log = [] ret = return_true(log, 3) assert ret is True assert 3 == len(log)
Example #4
Source File: conftest.py From nginx-proxy with MIT License | 5 votes |
def delete(self, *args, **kwargs): with ipv6(kwargs.pop('ipv6', False)): @backoff.on_predicate(backoff.constant, lambda r: r.status_code in (404, 502), interval=.3, max_tries=30, jitter=None) def _delete(*args, **kwargs): return self.session.delete(*args, **kwargs) return _delete(*args, **kwargs)
Example #5
Source File: conftest.py From nginx-proxy with MIT License | 5 votes |
def head(self, *args, **kwargs): with ipv6(kwargs.pop('ipv6', False)): @backoff.on_predicate(backoff.constant, lambda r: r.status_code in (404, 502), interval=.3, max_tries=30, jitter=None) def _head(*args, **kwargs): return self.session.head(*args, **kwargs) return _head(*args, **kwargs)
Example #6
Source File: conftest.py From nginx-proxy with MIT License | 5 votes |
def put(self, *args, **kwargs): with ipv6(kwargs.pop('ipv6', False)): @backoff.on_predicate(backoff.constant, lambda r: r.status_code in (404, 502), interval=.3, max_tries=30, jitter=None) def _put(*args, **kwargs): return self.session.put(*args, **kwargs) return _put(*args, **kwargs)
Example #7
Source File: conftest.py From nginx-proxy with MIT License | 5 votes |
def post(self, *args, **kwargs): with ipv6(kwargs.pop('ipv6', False)): @backoff.on_predicate(backoff.constant, lambda r: r.status_code in (404, 502), interval=.3, max_tries=30, jitter=None) def _post(*args, **kwargs): return self.session.post(*args, **kwargs) return _post(*args, **kwargs)
Example #8
Source File: conftest.py From nginx-proxy with MIT License | 5 votes |
def get(self, *args, **kwargs): with ipv6(kwargs.pop('ipv6', False)): @backoff.on_predicate(backoff.constant, lambda r: r.status_code in (404, 502), interval=.3, max_tries=30, jitter=None) def _get(*args, **kwargs): return self.session.get(*args, **kwargs) return _get(*args, **kwargs)
Example #9
Source File: test_backoff.py From backoff with MIT License | 5 votes |
def test_on_predicate_constant_iterable(monkeypatch): monkeypatch.setattr('time.sleep', lambda x: None) waits = [1, 2, 3, 6, 9] backoffs = [] giveups = [] successes = [] @backoff.on_predicate( backoff.constant, interval=waits, on_backoff=backoffs.append, on_giveup=giveups.append, on_success=successes.append, jitter=None, ) def falsey(): return False assert not falsey() assert len(backoffs) == len(waits) for i, wait in enumerate(waits): assert backoffs[i]['wait'] == wait assert len(giveups) == 1 assert len(successes) == 0
Example #10
Source File: test_backoff.py From backoff with MIT License | 5 votes |
def test_on_predicate_in_thread(monkeypatch): monkeypatch.setattr('time.sleep', lambda x: None) result = [] def check(): try: @backoff.on_predicate(backoff.expo) def return_true(log, n): val = (len(log) == n - 1) log.append(val) return val log = [] ret = return_true(log, 3) assert ret is True assert 3 == len(log) except Exception as ex: result.append(ex) else: result.append('success') t = threading.Thread(target=check) t.start() t.join() assert len(result) == 1 assert result[0] == 'success'
Example #11
Source File: test_backoff.py From backoff with MIT License | 5 votes |
def test_on_predicate_iterable_handlers(): class Logger: def __init__(self): self.backoffs = [] self.giveups = [] self.successes = [] loggers = [Logger() for _ in range(3)] @backoff.on_predicate(backoff.constant, on_backoff=(l.backoffs.append for l in loggers), on_giveup=(l.giveups.append for l in loggers), on_success=(l.successes.append for l in loggers), max_tries=3, jitter=None, interval=0) @_save_target def emptiness(*args, **kwargs): pass emptiness(1, 2, 3, foo=1, bar=2) for logger in loggers: assert len(logger.successes) == 0 assert len(logger.backoffs) == 2 assert len(logger.giveups) == 1 details = dict(logger.giveups[0]) print(details) elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': emptiness._target, 'tries': 3, 'value': None} # To maintain backward compatibility, # on_predicate should support 0-argument jitter function.
Example #12
Source File: test_backoff.py From backoff with MIT License | 5 votes |
def test_on_predicate_giveup(): backoffs, giveups, successes = [], [], [] @backoff.on_predicate(backoff.constant, on_success=successes.append, on_backoff=backoffs.append, on_giveup=giveups.append, max_tries=3, jitter=None, interval=0) @_save_target def emptiness(*args, **kwargs): pass emptiness(1, 2, 3, foo=1, bar=2) # we try 3 times, backing off twice and giving up once assert len(successes) == 0 assert len(backoffs) == 2 assert len(giveups) == 1 details = giveups[0] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': emptiness._target, 'tries': 3, 'value': None}
Example #13
Source File: test_backoff.py From backoff with MIT License | 5 votes |
def test_on_predicate_success(): backoffs, giveups, successes = [], [], [] @backoff.on_predicate(backoff.constant, on_success=successes.append, on_backoff=backoffs.append, on_giveup=giveups.append, jitter=None, interval=0) @_save_target def success(*args, **kwargs): # succeed after we've backed off twice return len(backoffs) == 2 success(1, 2, 3, foo=1, bar=2) # we try 3 times, backing off twice before succeeding assert len(successes) == 1 assert len(backoffs) == 2 assert len(giveups) == 0 for i in range(2): details = backoffs[i] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': success._target, 'tries': i + 1, 'value': False, 'wait': 0} details = successes[0] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': success._target, 'tries': 3, 'value': True}
Example #14
Source File: test_backoff.py From backoff with MIT License | 5 votes |
def test_on_predicate_max_time(monkeypatch): nows = [ datetime.datetime(2018, 1, 1, 12, 0, 10, 5), datetime.datetime(2018, 1, 1, 12, 0, 9, 0), datetime.datetime(2018, 1, 1, 12, 0, 1, 0), datetime.datetime(2018, 1, 1, 12, 0, 0, 0), ] class Datetime: @staticmethod def now(): return nows.pop() monkeypatch.setattr('time.sleep', lambda x: None) monkeypatch.setattr('datetime.datetime', Datetime) def giveup(details): assert details['tries'] == 3 assert details['elapsed'] == 10.000005 @backoff.on_predicate(backoff.expo, jitter=None, max_time=10, on_giveup=giveup) def return_true(log, n): val = (len(log) == n) log.append(val) return val log = [] ret = return_true(log, 10) assert ret is False assert len(log) == 3
Example #15
Source File: athena.py From streamalert with Apache License 2.0 | 5 votes |
def check_query_status(self, execution_id): """Check in on the running query, back off if the job is running or queued Args: query_execution_id (str): Athena query execution ID Returns: bool: True if the query state is SUCCEEDED, False otherwise Reference https://bit.ly/2uuRtda. Raises: AthenaQueryExecutionError: If any failure occurs while checking the status of the query, this exception will be raised """ LOGGER.debug('Checking status of query with execution ID: %s', execution_id) states_to_backoff = {'QUEUED', 'RUNNING'} @backoff.on_predicate(backoff.fibo, lambda resp: \ resp['QueryExecution']['Status']['State'] in states_to_backoff, max_value=10, jitter=backoff.full_jitter, on_backoff=backoff_handler(), on_success=success_handler(True)) def _check_status(query_execution_id): return self._client.get_query_execution( QueryExecutionId=query_execution_id ) execution_result = _check_status(execution_id) state = execution_result['QueryExecution']['Status']['State'] if state == 'SUCCEEDED': return # When the state is not SUCCEEDED, something bad must have occurred, so raise an exception reason = execution_result['QueryExecution']['Status']['StateChangeReason'] raise AthenaQueryExecutionError( 'Query \'{}\' {} with reason \'{}\', exiting'.format(execution_id, state, reason) )
Example #16
Source File: test_backoff_async.py From backoff with MIT License | 5 votes |
def test_on_predicate_iterable_handlers(): hdlrs = [_log_hdlrs() for _ in range(3)] @backoff.on_predicate(backoff.constant, on_success=(h[1] for h in hdlrs), on_backoff=(h[2] for h in hdlrs), on_giveup=(h[3] for h in hdlrs), max_tries=3, jitter=None, interval=0) @_save_target async def emptiness(*args, **kwargs): pass await emptiness(1, 2, 3, foo=1, bar=2) for i in range(3): assert len(hdlrs[i][0]['success']) == 0 assert len(hdlrs[i][0]['backoff']) == 2 assert len(hdlrs[i][0]['giveup']) == 1 details = dict(hdlrs[i][0]['giveup'][0]) elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': emptiness._target, 'tries': 3, 'value': None}
Example #17
Source File: test_backoff_async.py From backoff with MIT License | 5 votes |
def test_on_predicate_giveup(): log, log_success, log_backoff, log_giveup = _log_hdlrs() @backoff.on_predicate(backoff.constant, on_success=log_success, on_backoff=log_backoff, on_giveup=log_giveup, max_tries=3, jitter=None, interval=0) @_save_target async def emptiness(*args, **kwargs): pass await emptiness(1, 2, 3, foo=1, bar=2) # we try 3 times, backing off twice and giving up once assert len(log['success']) == 0 assert len(log['backoff']) == 2 assert len(log['giveup']) == 1 details = log['giveup'][0] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': emptiness._target, 'tries': 3, 'value': None}
Example #18
Source File: test_backoff_async.py From backoff with MIT License | 5 votes |
def test_on_predicate_success(): log, log_success, log_backoff, log_giveup = _log_hdlrs() @backoff.on_predicate(backoff.constant, on_success=log_success, on_backoff=log_backoff, on_giveup=log_giveup, jitter=None, interval=0) @_save_target async def success(*args, **kwargs): # succeed after we've backed off twice return len(log['backoff']) == 2 await success(1, 2, 3, foo=1, bar=2) # we try 3 times, backing off twice before succeeding assert len(log['success']) == 1 assert len(log['backoff']) == 2 assert len(log['giveup']) == 0 for i in range(2): details = log['backoff'][i] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': success._target, 'tries': i + 1, 'value': False, 'wait': 0} details = log['success'][0] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': success._target, 'tries': 3, 'value': True}
Example #19
Source File: test_backoff_async.py From backoff with MIT License | 5 votes |
def test_on_predicate_max_tries(monkeypatch): monkeypatch.setattr('asyncio.sleep', _await_none) @backoff.on_predicate(backoff.expo, jitter=None, max_tries=3) async def return_true(log, n): val = (len(log) == n) log.append(val) return val log = [] ret = await return_true(log, 10) assert ret is False assert 3 == len(log)
Example #20
Source File: test_backoff_async.py From backoff with MIT License | 5 votes |
def test_on_predicate(monkeypatch): monkeypatch.setattr('asyncio.sleep', _await_none) @backoff.on_predicate(backoff.expo) async def return_true(log, n): val = (len(log) == n - 1) log.append(val) return val log = [] ret = await return_true(log, 3) assert ret is True assert 3 == len(log)
Example #21
Source File: _deploygke.py From django-cloud-deploy with Apache License 2.0 | 5 votes |
def _try_get_ready_replicas(self, api: kubernetes.client.ExtensionsV1beta1Api, label_selector: str) -> int: """Return ready replicas when deployment is ready.""" items = api.list_deployment_for_all_namespaces( label_selector=label_selector).items for item in items: if item.status.ready_replicas: return item.status.ready_replicas # @backoff.on_predicate(backoff.constant) will keep running this method # until it gets a non-falsey result. Return value of 0 means that the # deployment is not ready yet. return 0
Example #22
Source File: _deploygke.py From django-cloud-deploy with Apache License 2.0 | 5 votes |
def _try_get_ingress_url(self, api: kubernetes.client.CoreV1Api) -> str: """Return Ingress url when service is ready.""" items = api.list_service_for_all_namespaces().items for item in items: ingress = item.status.load_balancer.ingress if ingress: return 'http://{}/'.format(ingress[0].hostname or ingress[0].ip) # @backoff.on_predicate(backoff.constant) will keep running this method # until it gets a non-falsey result. Return value of '' means that the # service is not ready yet. return ''
Example #23
Source File: test_backoff_async.py From backoff with MIT License | 4 votes |
def test_on_predicate_success_0_arg_jitter(monkeypatch): monkeypatch.setattr('asyncio.sleep', _await_none) monkeypatch.setattr('random.random', lambda: 0) log, log_success, log_backoff, log_giveup = _log_hdlrs() @backoff.on_predicate(backoff.constant, on_success=log_success, on_backoff=log_backoff, on_giveup=log_giveup, jitter=random.random, interval=0) @_save_target async def success(*args, **kwargs): # succeed after we've backed off twice return len(log['backoff']) == 2 with pytest.deprecated_call(): await success(1, 2, 3, foo=1, bar=2) # we try 3 times, backing off twice before succeeding assert len(log['success']) == 1 assert len(log['backoff']) == 2 assert len(log['giveup']) == 0 for i in range(2): details = log['backoff'][i] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': success._target, 'tries': i + 1, 'value': False, 'wait': 0} details = log['success'][0] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': success._target, 'tries': 3, 'value': True}
Example #24
Source File: test_backoff_async.py From backoff with MIT License | 4 votes |
def test_on_exception_success_0_arg_jitter(monkeypatch): monkeypatch.setattr('asyncio.sleep', _await_none) monkeypatch.setattr('random.random', lambda: 0) log, log_success, log_backoff, log_giveup = _log_hdlrs() @backoff.on_exception(backoff.constant, Exception, on_success=log_success, on_backoff=log_backoff, on_giveup=log_giveup, jitter=random.random, interval=0) @_save_target async def succeeder(*args, **kwargs): # succeed after we've backed off twice if len(log['backoff']) < 2: raise ValueError("catch me") with pytest.deprecated_call(): await succeeder(1, 2, 3, foo=1, bar=2) # we try 3 times, backing off twice before succeeding assert len(log['success']) == 1 assert len(log['backoff']) == 2 assert len(log['giveup']) == 0 for i in range(2): details = log['backoff'][i] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': succeeder._target, 'tries': i + 1, 'wait': 0} details = log['success'][0] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': succeeder._target, 'tries': 3} # To maintain backward compatibility, # on_predicate should support 0-argument jitter function.
Example #25
Source File: test_backoff.py From backoff with MIT License | 4 votes |
def test_on_exception_success_0_arg_jitter(monkeypatch): monkeypatch.setattr('time.sleep', lambda x: None) monkeypatch.setattr('random.random', lambda: 0) backoffs, giveups, successes = [], [], [] @backoff.on_exception(backoff.constant, Exception, on_success=successes.append, on_backoff=backoffs.append, on_giveup=giveups.append, jitter=random.random, interval=0) @_save_target def succeeder(*args, **kwargs): # succeed after we've backed off twice if len(backoffs) < 2: raise ValueError("catch me") with pytest.deprecated_call(): succeeder(1, 2, 3, foo=1, bar=2) # we try 3 times, backing off twice before succeeding assert len(successes) == 1 assert len(backoffs) == 2 assert len(giveups) == 0 for i in range(2): details = backoffs[i] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': succeeder._target, 'tries': i + 1, 'wait': 0} details = successes[0] elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, 'target': succeeder._target, 'tries': 3} # To maintain backward compatibility, # on_predicate should support 0-argument jitter function.
Example #26
Source File: firehose.py From streamalert with Apache License 2.0 | 4 votes |
def _send_batch(self, stream_name, record_batch): """Send record batches to Firehose Args: stream_name (str): The name of the Delivery Stream to send to record_batch (list): The records to send """ @backoff.on_predicate(backoff.fibo, lambda resp: resp['FailedPutCount'] > 0, max_tries=self.MAX_BACKOFF_ATTEMPTS, max_value=self.MAX_BACKOFF_FIBO_VALUE, on_backoff=backoff_handler(debug_only=False), on_success=success_handler(), on_giveup=giveup_handler()) @backoff.on_exception(backoff.fibo, self.EXCEPTIONS_TO_BACKOFF, max_tries=self.MAX_BACKOFF_ATTEMPTS, on_backoff=backoff_handler(debug_only=False), on_success=success_handler(), on_giveup=giveup_handler()) def _firehose_request_helper(data): """Firehose request wrapper to use with backoff""" # Use the current length of data here so we can track failed records that are retried LOGGER.debug('Sending %d records to firehose %s', len(data), stream_name) response = self._client.put_record_batch(DeliveryStreamName=stream_name, Records=data) # Log this as an error for now so it can be picked up in logs if response['FailedPutCount'] > 0: LOGGER.warning('Received non-zero FailedPutCount: %d', response['FailedPutCount']) # Strip out the successful records so only the failed ones are retried. This happens # to the list of dictionary objects, so the called function sees the updated list self._strip_successful_records(data, response) return response # The record here already contains a newline, so do not append one records_data = [ {'Data': record} for record in record_batch ] # The try/except here is to catch the raised error at the end of the backoff try: return _firehose_request_helper(records_data) except self.EXCEPTIONS_TO_BACKOFF: LOGGER.exception('Firehose request failed') # Use the current length of the records_data in case some records were # successful but others were not self._log_failed(len(records_data))