public
Last active

__retry decorator

  • Download Gist
client_with_retry.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class ApiClient(object):
default_retries = (1, 2, 5, 10, 20, 30)
 
def __retry(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
retries = kwargs.pop('retries', self.default_retries)
try:
if kwargs.pop('reconnect', False):
self.__init__(self.ftp_uri, retries=())
return func(self, *args, **kwargs)
except (IOError, OSError, EOFError):
if retries:
#exc_type, exc_value, exc_tb = sys.exc_info()
# TODO: strip 2 upper entries from exc_tb cause they are
# just wrapper calls
logger.exception(
"%s(%s, %s) raised exception. %s retries left. Sleeping %s secs. Caller traceback: %s\nInternal traceback:",
func.__name__, args, kwargs, len(retries), retries[0],
''.join(traceback.format_stack(
f=sys._getframe(
1 + len(self.default_retries) - len(retries)
),
limit=10,
)),
)
time.sleep(retries[0])
kwargs['retries'] = retries[1:]
kwargs['reconnect'] = True
 
return wrapper(self, *args, **kwargs)
else:
raise
return wrapper
 
@__retry
def __init__(self, uri):
self.uri = uri
# open connection to uri, blabla
 
@__retry
def do_stuff(self):
# eg: do stuff with self.connection
pass

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.