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

Add retry decorator and function wrapper #17

Merged
merged 3 commits into from
Dec 12, 2020
Merged

Add retry decorator and function wrapper #17

merged 3 commits into from
Dec 12, 2020

Conversation

taldcroft
Copy link
Member

Description

Being able to flexibly retry certain function calls is a useful feature. I found this retry package (https://github.com/invl/retry), which is basically what we want, except:

  • It appears to be abandoned and hasn't had any releases in 4 years.
  • The details weren't quite right, specifically the default message for a failure.

So I copied the whole thing into ska_helpers/retry in the first commit, then added provenance and license information, then made it to my liking.

Testing

  • Passes unit tests on MacOS
  • Functional testing

Functional testing

from ska_helpers.retry import retry

@retry(tries=3, delay=0.5, backoff=4)
def func(a, b=1):
    print(f'hey {a} {b}')
    raise Exception('hello')

>>> func(3, b=5)                                                                                                   
hey 3 5
WARNING: func(3, b=5) exception: hello, retrying in 0.5 seconds...
hey 3 5
WARNING: func(3, b=5) exception: hello, retrying in 2.0 seconds...
hey 3 5
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-2-faa7e5b4124a> in <module>
----> 1 func(3, b=5)

~/git/ska_helpers/ska_helpers/retry/api.py in wrapper(*args, **kwargs)
     77         @functools.wraps(f)
     78         def wrapper(*args, **kwargs):
---> 79             return __retry_internal(f, exceptions, tries, delay, max_delay,
     80                                     backoff, jitter, logger, args=args, kwargs=kwargs)
     81         return wrapper

~/git/ska_helpers/ska_helpers/retry/api.py in __retry_internal(f, exceptions, tries, delay, max_delay, backoff, jitter, logger, args, kwargs)
     30     while _tries:
     31         try:
---> 32             return f(*args, **kwargs)
     33         except exceptions as e:
     34             _tries -= 1

~/git/ska_helpers/go.py in func(a, b)
      4 def func(a, b=1):
      5     print(f'hey {a} {b}')
----> 6     raise Exception('hello')

Exception: hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants