Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebase core/opl and cleanup #129

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/opl/cluster_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from . import data
from . import date
from . import status_data
from . import skelet
from . import retry


def execute(command):
Expand Down Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions core/opl/retry.py
Original file line number Diff line number Diff line change
@@ -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
41 changes: 0 additions & 41 deletions core/opl/skelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import time
from contextlib import contextmanager
from functools import wraps

from . import status_data

Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion opl/cluster_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from . import data
from . import date
from . import status_data
from . import skelet
from . import retry


Expand Down
1 change: 0 additions & 1 deletion tests/test_retry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

import unittest
import argparse
import datetime

from .context import opl
Expand Down
1 change: 0 additions & 1 deletion tests/test_skelet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import unittest
import argparse
import datetime

from .context import opl

Expand Down
Loading