retry strategy triggered by any Exception except the ones listed
Hi, thanks for this project guys!
I was wondering if is there a way to create a retry strategy that instead of retrying for some listed exceptions it'd retry for any Exception except the ones listed, so basically it's the inverse logic currently used.
I was checking the BaseRetryStrategy class but it seems to me that it doesn't manage the logic for selecting which exceptions should be retried.
Thanks.
I think we could introduce a no_retry_exceptions parameter, and do this in the get_schedule_in function:
def get_schedule_in(self, *, exception: Exception, attempts: int) -> Optional[int]:
if self.max_attempts and attempts >= self.max_attempts:
return None
if self.retry_exceptions and not isinstance(
exception,
tuple(self.retry_exceptions)
):
return None
if self.no_retry_exceptions and isinstance(
exception,
tuple(self.no_retry_exceptions)
):
return None
…
Not that with this code if the exception is both a "retry exception" and a "no retry exception" then there will be no retry. And if the exception is neither a "retry exception" nor a "no retry exception" there will be no retry either. In other words if there's ambiguity "no retry" is favored over "retry".
(I think we could raise if retry_exceptions and no_retry_exceptions are used at the same time.)
In the face of ambiguity, refuse the temptation to guess.
could this be implemented just subclassing BaseRetryStrategy then?
(I think we could raise if
retry_exceptionsandno_retry_exceptionsare used at the same time.)
Yep, agree.
could this be implemented just subclassing BaseRetryStrategy then?
Yes I think so. Though it make sense to add this to the procrastinate lib, feel free to implement this on your side in the meantime. And/or to contribute to the lib :)
See https://docs.python.org/3/library/typing.html#typing.overload to properly indicate with mypy that arguments are mutually exclusive