channable / opnieuw

One weird trick to make your code more reliable

Home Page:https://tech.channable.com/posts/2020-02-05-opnieuw.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for callbacks

michaeloliverx opened this issue · comments

It would be nice to have support for a callback (sync or async) which is called once the retry limit was reached.

Btw thanks for the library

The last thrown exception is raised once the retry window is over or the maximum number of retries is reached.

opnieuw/opnieuw/retries.py

Lines 247 to 248 in 480142d

if last_exception is not None:
raise last_exception

You could create a decorator that catches the exceptions you would like your callback to handle. The new decorator, for simplicity named add_callback, should be the outside the retry decorator if you want to retry the exception and only handle unresolved exceptions in your callback. The add_callback decorator should be inside the retry decorator if you like to handle each of the exceptions before retrying.

def add_callback(
    *,
    callback_on_exceptions: Union[Type[Exception], Tuple[Type[Exception], ...]],
    callback: Callable[Union[Type[Exception], Tuple[Type[Exception], ...]], Any],
) -> Callable[[F], F]:
    def decorator(f: F) -> F:
        @functools.wraps(f)
        def wrapper(*args: Any, **kwargs: Any) -> Any:
            try:
                return f(*args, **kwargs)
            except callback_on_exceptions as e:
                callback(e)
				raise e
        return cast(F, wrapper)
    return decorator

Thanks for using our library!
Hopefully, this helps to handle the problems you encounter.