diff --git a/core/opl/cluster_read.py b/core/opl/cluster_read.py index f94a2f5..dfa718a 100755 --- a/core/opl/cluster_read.py +++ b/core/opl/cluster_read.py @@ -16,7 +16,7 @@ from . import data from . import date from . import status_data -from . import skelet +from . import retry def execute(command): @@ -181,7 +181,7 @@ def _sanitize_target(self, target): target = target.replace("$Cloud", self.args.grafana_prefix) return target - @skelet.retry_on_traceback(max_attempts=10, wait_seconds=1) + @retry.retry_on_traceback(max_attempts=10, wait_seconds=1) def measure(self, ri, name, grafana_target): assert ( ri.start is not None and ri.end is not None diff --git a/core/opl/retry.py b/core/opl/retry.py new file mode 100644 index 0000000..baaf67e --- /dev/null +++ b/core/opl/retry.py @@ -0,0 +1,43 @@ +import logging +import time +from functools import wraps + + +def retry_on_traceback(max_attempts=10, wait_seconds=1): + """ + Retries a function until it succeeds or the maximum number of attempts + or wait time is reached. + + This is to mimic `@retry` decorator from Tenacity so we do not depend + on it. + + Args: + max_attempts: The maximum number of attempts to retry the function. + wait_seconds: The number of seconds to wait between retries. + + Returns: + A decorator that retries the wrapped function. + """ + assert max_attempts >= 0, "It does not make sense to have less than 0 retries" + assert wait_seconds >= 0, "It does not make sense to wait les than 0 seconds" + + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + attempt = 0 + while True: + try: + return func(*args, **kwargs) + except Exception as e: + if attempt >= max_attempts: + raise # Reraise the exception after all retries are exhausted + + attempt += 1 + logging.debug( + f"Retrying in {wait_seconds} seconds. Attempt {attempt}/{max_attempts} failed with: {e}" + ) + time.sleep(wait_seconds) + + return wrapper + + return decorator diff --git a/core/opl/skelet.py b/core/opl/skelet.py index 771381f..f83700c 100644 --- a/core/opl/skelet.py +++ b/core/opl/skelet.py @@ -3,7 +3,6 @@ import os import time from contextlib import contextmanager -from functools import wraps from . import status_data @@ -86,43 +85,3 @@ def test_setup(parser, logger_name="root"): yield (args, sdata) finally: sdata.save() - - -def retry_on_traceback(max_attempts=10, wait_seconds=1): - """ - Retries a function until it succeeds or the maximum number of attempts - or wait time is reached. - - This is to mimic `@retry` decorator from Tenacity so we do not depend - on it. - - Args: - max_attempts: The maximum number of attempts to retry the function. - wait_seconds: The number of seconds to wait between retries. - - Returns: - A decorator that retries the wrapped function. - """ - assert max_attempts >= 0, "It does not make sense to have less than 0 retries" - assert wait_seconds >= 0, "It does not make sense to wait les than 0 seconds" - - def decorator(func): - @wraps(func) - def wrapper(*args, **kwargs): - attempt = 0 - while True: - try: - return func(*args, **kwargs) - except Exception as e: - if attempt >= max_attempts: - raise # Reraise the exception after all retries are exhausted - - attempt += 1 - logging.debug( - f"Retrying in {wait_seconds} seconds. Attempt {attempt}/{max_attempts} failed with: {e}" - ) - time.sleep(wait_seconds) - - return wrapper - - return decorator diff --git a/opl/cluster_read.py b/opl/cluster_read.py index b28cac0..dfa718a 100755 --- a/opl/cluster_read.py +++ b/opl/cluster_read.py @@ -16,7 +16,6 @@ from . import data from . import date from . import status_data -from . import skelet from . import retry diff --git a/tests/test_retry.py b/tests/test_retry.py index 2d22d39..cc846c2 100644 --- a/tests/test_retry.py +++ b/tests/test_retry.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import unittest -import argparse import datetime from .context import opl diff --git a/tests/test_skelet.py b/tests/test_skelet.py index ac970ba..1e4cb10 100644 --- a/tests/test_skelet.py +++ b/tests/test_skelet.py @@ -2,7 +2,6 @@ import unittest import argparse -import datetime from .context import opl