Skip to content

Instantly share code, notes, and snippets.

@ionelmc
Last active December 14, 2015 09:18
Show Gist options
  • Save ionelmc/5063601 to your computer and use it in GitHub Desktop.
Save ionelmc/5063601 to your computer and use it in GitHub Desktop.
__retry decorator
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment