retry_on_err¶
-
privex.helpers.decorators.
retry_on_err
(max_retries: int = 3, delay: Union[int, float] = 3, **retry_conf)[source]¶ Decorates a function or class method, wraps the function/method with a try/catch block, and will automatically re-run the function with the same arguments up to max_retries time after any exception is raised, with a
delay
second delay between re-tries.If it still throws an exception after
max_retries
retries, it will log the exception details withfail_msg
, and then re-raise it.Usage (retry up to 5 times, 1 second between retries, stop immediately if IOError is detected):
>>> @retry_on_err(5, 1, fail_on=[IOError]) ... def my_func(self, some=None, args=None): ... if some == 'io': raise IOError() ... raise FileExistsError()
This will be re-ran 5 times, 1 second apart after each exception is raised, before giving up:
>>> my_func()
Where-as this one will immediately re-raise the caught IOError on the first attempt, as it’s passed in
fail_on
:>>> my_func('io')
Attention
For safety reasons, by default
max_ignore
is set to100
. This means after 100 retries where an exception was ignored, the decorator will give up and raise the last exception.This is to prevent the risk of infinite loops hanging your application. If you are 100% certain that the function you’ve wrapped, and/or the exceptions passed in
ignore
cannot cause an infinite retry loop, then you can passmax_ignore=False
to the decorator to disable failure aftermax_ignore
ignored exceptions.- Parameters
- Key list fail_on
A list() of Exception types that should result in immediate failure (don’t retry, raise)
- Key list ignore
A list() of Exception types that should be ignored (will retry, but without incrementing the failure counter)
- Key int|bool max_ignore
(Default:
100
) If an exception is raised while retrying, and more than this many exceptions (listed inignore
) have been ignored during retry attempts, then give up and raise the last exception.This feature is designed to prevent “ignored” exceptions causing an infinite retry loop. By default
max_ignore
is set to100
, but you can increase/decrease this as needed.You can also set it to
False
to disable raising when too many exceptions are ignored - however, it’s strongly not recommended to disablemax_ignore
, especially if you haveinstance_match=True
, as it could cause an infinite retry loop which hangs your application.- Key bool instance_match
(Default:
False
) If this is set toTrue
, then the exception type comparisons forfail_on
andignore
will compare usingisinstance(e, x)
instead oftype(e) is x
.If this is enabled, then exceptions listed in
fail_on
andignore
will also match sub-classes of the listed exceptions, instead of exact matches.- Key str retry_msg
Override the log message used for retry attempts. First message param %s is func name, second message param %d is retry attempts remaining
- Key str fail_msg
Override the log message used after all retry attempts are exhausted. First message param %s is func name, and second param %d is amount of times retried.