diff --git a/src/omnia_timeseries/helpers.py b/src/omnia_timeseries/helpers.py index ae2385e..b7a18cc 100644 --- a/src/omnia_timeseries/helpers.py +++ b/src/omnia_timeseries/helpers.py @@ -1,12 +1,19 @@ from functools import wraps +import logging import time +from typing import Optional from omnia_timeseries.models import TimeseriesRequestFailedException # Reasonable status codes to retry, based on descriptions at https://en.wikipedia.org/wiki/List_of_HTTP_status_codes retry_status_codes = [408, 409, 425, 429, 502, 503, 504] -def retry(total_tries=3, initial_wait=0.5, backoff_factor=2, logger=None): +def retry( + total_tries=3, + initial_wait=0.5, + backoff_factor=2, + logger: Optional[logging.Logger] = None, +): def retry_decorator(f): @wraps(f) def func_with_retries(*args, **kwargs): @@ -20,18 +27,12 @@ def func_with_retries(*args, **kwargs): except TimeseriesRequestFailedException as e: _tries -= 1 if e.status_code in retry_status_codes: - if _tries == 0: - msg = str( - f"Function: {f.__name__} Failed despite best efforts after {total_tries} tries." - ) - if logger is not None: - logger.warning(msg) - else: - msg = str( - f"Function: {f.__name__} failed with {e}. Retrying in {_delay} seconds, with {_tries} retries remaining!\n" - ) - if logger is not None: - logger.warning(msg) + if logger is not None: + if _tries == 0: + msg = f"Function: {f.__name__} Failed despite best efforts after {total_tries} tries." + else: + msg = f"Function: {f.__name__} failed with {e}. Retrying in {_delay} seconds, with {_tries} retries remaining!" + logger.warning(msg) time.sleep(_delay) _delay *= backoff_factor exception = e