channable / opnieuw

One weird trick to make your code more reliable
https://tech.channable.com/posts/2020-02-05-opnieuw.html
BSD 3-Clause "New" or "Revised" License
289 stars 11 forks source link

Works in Try block? #4

Closed jaoxford closed 4 years ago

jaoxford commented 4 years ago

Hi there,

I'm curious to know if this library will work when a function is called from within a try block? If an exception happens inside the function being called will the except block be called, or only if the retries happen to fail?

try:
    foo()
except Exception:
    print(Exception)

@retry(
    retry_on_exceptions=(FooException),
    max_calls_total=3,
    retry_window_after_first_call_in_seconds=60,
)
def foo():
ReinierMaas commented 4 years ago

This pattern is used in the testcases.

https://github.com/channable/opnieuw/blob/dc7ac018d477eb2a193353322680e18b2848cfb9/tests/test_opnieuw.py#L41-L69

As we can see from the assert statements the counter is incremented to 3 before the block in the except statement is called.

If you want to log the exceptions that occur in foo they should be logged from the inside.

@retry(
    retry_on_exceptions=(FooException),
    max_calls_total=3,
    retry_window_after_first_call_in_seconds=60,
)
def wrap_foo:
    try:
        foo()
    except Exception as e:
        log(e)
        raise e

def foo:
    pass
jaoxford commented 4 years ago

Lovely, thank you!