-
Notifications
You must be signed in to change notification settings - Fork 152
Open
Description
I want to decorate methods of a class with @Retry and use a specific method as the "retry_on_exception" function. Issue is of course, that you can not do
@retry(retry_on_exception=self.retry_handler)since self is not defined in that case. This code snippet is an attempt to work around this:
def method_retry(*dargs, **dkw):
def decorator_retry(func):
@functools.wraps(func)
def retry_func(self, *args, **kwargs):
fixed_dkw = dkw.copy()
if retry_on_ex := fixed_dkw.get("retry_on_exception"):
fixed_dkw["retry_on_exception"] = lambda ex: retry_on_ex(self, ex)
if retry_on_res := fixed_dkw.get("wait_on_result"):
fixed_dkw["retry_on_result"] = lambda res: retry_on_res(self, res)
return Retrying(*dargs, **fixed_dkw).call(func, self, *args, **kwargs)
return retry_func
return decorator_retry*dargs would have to be modified similarly to kwargs. There might be an even more elegant solution though. But this is the best I could come up with so far.
This seems to work, although you can not do
class MyClass:
def my_exception_handler(self):
pass
@method_retry(retry_on_exception=MyClass.my_exception_handler)
def my_method(self):
passsince MyClass is not defined at that point yet.
so you have to do
def my_exception_handler(self):
pass
class MyClass:
@method_retry(retry_on_exception=my_exception_handler)
def my_method(self):
passwhich is quite annoying.
Would be cool if anyone had a better idea.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels