procrastinate icon indicating copy to clipboard operation
procrastinate copied to clipboard

retry strategy triggered by any Exception except the ones listed

Open chespinoza opened this issue 4 years ago • 6 comments

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.

chespinoza avatar Nov 04 '21 17:11 chespinoza

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".

elemoine avatar Nov 05 '21 07:11 elemoine

(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.

ewjoachim avatar Nov 05 '21 10:11 ewjoachim

could this be implemented just subclassing BaseRetryStrategy then?

chespinoza avatar Nov 05 '21 10:11 chespinoza

(I think we could raise if retry_exceptions and no_retry_exceptions are used at the same time.)

Yep, agree.

elemoine avatar Nov 05 '21 13:11 elemoine

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 :)

ewjoachim avatar Nov 05 '21 16:11 ewjoachim

See https://docs.python.org/3/library/typing.html#typing.overload to properly indicate with mypy that arguments are mutually exclusive

ewjoachim avatar Nov 05 '21 17:11 ewjoachim