Created
August 14, 2016 19:31
-
-
Save sintezcs/f131afd05c5eb9dc35aad6e095a310f5 to your computer and use it in GitHub Desktop.
Decorator for retrying function call in case of failure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def retry(error_condition=lambda err: True, | |
retry_count=10, retry_timeout=1, progressive_timeout=True): | |
"""Decorator for retrying function call in case of exception | |
You could decorate any function or method with this if you need to | |
repeatedly call this method a couple of times with an increasing interval | |
in case of some error raised during the method call. | |
Args: | |
error_condition (callable(error)): Function that will check whether | |
we should do retries for a particular error. E.g. you can check | |
error class, some it's fields or values. | |
retry_count (int): Number ot retries | |
retry_timeout (int): Timeout in seconds to wait between retries | |
progressive_timeout (bool): If True, the timeout value will be | |
increased by 0.5 sec during each consecutive retry | |
Raises: | |
RetryCountExceededError: | |
Error is being raised in case of retry count exceeded | |
""" | |
def wrapper(fn): | |
@wraps(fn) | |
def wrapped(*args, **kwargs): | |
retries = 0 | |
timeout = retry_timeout | |
while retries < retry_count: | |
try: | |
result = fn(*args, **kwargs) | |
return result | |
except Exception as err: | |
if error_condition(err): | |
time.sleep(timeout) | |
retries += 1 | |
timeout += 0.5 if progressive_timeout else 0 | |
else: | |
raise err | |
raise RetryCountExceededError | |
return wrapped | |
return wrapper | |
class RetryCountExceededError(Exception): | |
"""Raised by @retry decorator when it exceeds the defined retry count | |
""" | |
def __str__(self): | |
return 'Method retry count exceeded' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment