privex.helpers.decorators.retry_on_err¶
-
privex.helpers.decorators.
retry_on_err
(max_retries: int = 3, delay: int = 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')
- Parameters
(list) fail_on: A list() of Exception types that should result in immediate failure (don’t retry, raise)
(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
(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.