diff --git a/src/common/tools/client/RetryDecorator.py b/src/common/tools/client/RetryDecorator.py index fc45d95c8dccbbca32ace5fbba5fb4aefc7a4d79..4750ff73ae4342ce2eb2a31941ff48b46e5be281 100644 --- a/src/common/tools/client/RetryDecorator.py +++ b/src/common/tools/client/RetryDecorator.py @@ -51,24 +51,32 @@ LOGGER = logging.getLogger(__name__) def delay_linear(initial=0, increment=0, maximum=None): def compute(num_try): delay = initial + (num_try - 1) * increment - if maximum is not None: delay = max(delay, maximum) + if maximum is not None: + delay = max(delay, maximum) return delay return compute def delay_exponential(initial=1, increment=1, maximum=None): def compute(num_try): delay = initial * pow(increment, (num_try - 1)) - if maximum is not None: delay = max(delay, maximum) + if maximum is not None: + delay = max(delay, maximum) return delay return compute -def retry(max_retries=0, delay_function=delay_linear(initial=0, increment=0), - prepare_method_name=None, prepare_method_args=[], prepare_method_kwargs={}): +# pylint: disable=dangerous-default-value +def retry( + max_retries=0, delay_function=delay_linear(initial=0, increment=0), + prepare_method_name=None, prepare_method_args=list(), prepare_method_kwargs=dict() +): def _reconnect(func): def wrapper(self, *args, **kwargs): if prepare_method_name is not None: prepare_method = getattr(self, prepare_method_name, None) - if prepare_method is None: raise Exception('Prepare Method ({}) not found'.format(prepare_method_name)) + if prepare_method is None: + MSG = 'Prepare Method ({:s}) not found' + # pylint: disable=broad-exception-raised + raise Exception(MSG.format(prepare_method_name)) num_try, given_up = 0, False while not given_up: try: @@ -78,14 +86,29 @@ def retry(max_retries=0, delay_function=delay_linear(initial=0, increment=0), num_try += 1 given_up = num_try > max_retries - if given_up: raise Exception('Giving up... {:d} tries failed'.format(max_retries)) from e + if given_up: + MSG = '[{:s}:{:s}] Giving up... {:d} tries failed' + msg = MSG.format(func.__module__, func.__name__, max_retries) + # pylint: disable=broad-exception-raised + raise Exception(msg) from e if delay_function is not None: delay = delay_function(num_try) time.sleep(delay) - LOGGER.info('Retry {:d}/{:d} after {:f} seconds...'.format(num_try, max_retries, delay)) + MSG = '[{:s}:{:s}] Retry {:d}/{:d} after {:f} seconds...' + LOGGER.info(MSG.format( + func.__module__, func.__name__, num_try, max_retries, delay + )) else: - LOGGER.info('Retry {:d}/{:d} immediate...'.format(num_try, max_retries)) + MSG = '[{:s}:{:s}] Retry {:d}/{:d} immediate...' + LOGGER.info(MSG.format( + func.__module__, func.__name__, num_try, max_retries + )) - if prepare_method_name is not None: prepare_method(*prepare_method_args, **prepare_method_kwargs) + if prepare_method_name is not None: + MSG = '[{:s}:{:s}] Running prepare method...' + LOGGER.debug(MSG.format( + prepare_method.__module__, prepare_method.__name__ + )) + prepare_method(*prepare_method_args, **prepare_method_kwargs) return wrapper return _reconnect