-
Notifications
You must be signed in to change notification settings - Fork 62
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 hooks for @retry
decorator
#162
Comments
@prkumar this issue is really problematic for me. Is there any way I can help with this? You think this would make a good issue to contribute to? |
@liiight I've added the Critical label to this issue and pinned it, so I'll prioritize the work for this one! If you'd like to tackle a solution, I'd suggest looking at the Lines 33 to 45 in ff72f3f
For both method, the Possible WorkaroundHere's a workaround that you can use right now: define your own For example, here's a very simple implementation for a retry counter: from uplink.retry.when import RetryPredicate
class RetryPredicateWithCounter(RetryPredicate):
def __init__(self, predicate):
self._predicate = predicate # This should be a `retry.when.*` predicate
self._retry_count = 0
def should_retry_after_response(self, response):
should_retry = self._predicate.should_retry_after_response(response)
self_retry_count = 0 if not should_retry else self_retry_count + 1
return result
def should_retry_after_exception(self, exc_type, exc_val, exc_tb):
should_retry = self._predicate.should_retry_after_exception(exc_type, exc_val, exc_tb)
self_retry_count = 0 if not should_retry else self_retry_count + 1
return result Then, you would pass a @retry(when=RetryPredicateWithCounter(retry.when.raises(MyException)))
@get('users')
def get_users(self):
pass |
Thanks for the workaround the pointers! I'd like to run another design option other than those you suggested, more consistent with response handler (which i really like) @uplink.retry_handler
def a_retry_handler(retry_exception, response):
if response.status_code == 404:
do_something()
return response # maybe raise the retry exception?
@a_retry_handler
@retry(...)
@get('users')
def get_users(self):
pass WDYT? |
@liiight - The Have you had a chance to try out the workaround I mentioned in my previous comment? It should work with the latest version of the library. |
I ended up writing my own class FutureIsNotCompleted(retry.when.RetryPredicate):
# noinspection PyMethodMayBeStatic
def should_retry_after_response(self, response):
try:
res_json = response.json()
except ValueError:
res_json = [{}]
# status: Completed, Inprogress
return not all(outer.get("status") == "Completed" for outer in res_json) class FuturesMixin: # meant to be inherited by a class that also inherits from uplink.Consumer
@retry(when=FutureIsNotCompleted(), stop=retry.stop.after_delay(240))
@returns_results_or_future
@get(args=(Url,))
def _check_async_results(self, location):
pass |
Is your feature request related to a problem? Please describe.
This FR is from @liiight on gitter:
Describe the solution you'd like
One way we could approach this is with a
on_retry
argument for the@retry
decorator. This would be a function that is called when the retry condition is met, and it would get the most recent exception or failed response on each invocation:Alternatively, we could add the hook using a
@retry.handler
decorator:The text was updated successfully, but these errors were encountered: