- class wmflib.decorators.RetryParams(tries: int, delay: datetime.timedelta, backoff_mode: str, exceptions: Tuple[Type[Exception], ...], failure_message: str)¶
Retry decorator parameters object.
- wmflib.decorators.retry(func: Callable, *, tries: int = 3, delay: datetime.timedelta = datetime.timedelta(seconds=3), backoff_mode: str = 'exponential', exceptions: Tuple[Type[Exception], ...] = (<class 'wmflib.exceptions.WmflibError'>,), failure_message: Optional[str] = None, dynamic_params_callbacks: Tuple[Callable[[wmflib.decorators.RetryParams, Callable, Tuple, Dict[str, Any]], None], ...] = ()) Callable ¶
Decorator to retry a function or method if it raises certain exceptions with customizable backoff.
The decorated function or method must be idempotent to avoid unwanted side effects. It can be called with or without arguments, in the latter case all the default values will be used.
func (function, method) – the decorated function.
tries (int, optional) – the number of times to try calling the decorated function or method before giving up. Must be a positive integer.
delay (datetime.timedelta, optional) – the initial delay for the first retry, used also as the base for the backoff algorithm.
backoff_mode (str, optional) –
the backoff mode to use for the delay, available values are:
constant: delay => 3, 3, 3, 3, 3, ...; linear: delay * N => 3, 6, 9, 12, 15, ...; N in [1, tries] power: delay * 2^N => 3, 6, 12, 24, 48, ...; N in [0, tries - 1] exponential: delay^N => 3, 9, 27, 81, 243, ...; N in [1, tries], delay must be > 1.
exceptions (type, tuple, optional) – the decorated function call will be retried if it fails until it succeeds or tries attempts are reached. A retryable failure is defined as raising any of the exceptions listed.
failure_message (str, optional) – the message to log each time there’s a retryable failure. Retry information and exception message are also included. Default: “Attempt to run ‘<fully qualified function>’ raised”
dynamic_params_callbacks (tuple) –
a tuple of callbacks that will be called at runtime to allow to modify the decorator’s parameters. Each callable must adhere to the following interface:
def adjust_some_parameter(retry_params: RetryParams, func: Callable, args: Tuple, kwargs: Dict) -> None # Modify the retry_params parameter possibly using the decorated function object or its parameters # that are passed as tuple for the positional arguments and a dictionary for the keyword arguments
This is a practical example that defines a callback that doubles the delay parameter of the
@retrydecorator if the decorated function/method has a ‘slow’ keyword argument that is to True:
def double_delay(retry_params, func, args, kwargs): if kwargs.get('slow', False): retry_params.delay = retry_params.delay * 2 @retry(delay=timedelta(seconds=10), dynamic_params_callbacks=(double_delay,)) def do_something(slow=False): # This method will be retried using 10 seconds as delay parameter in the @retry decorator, but # if the 'slow' parameter is set to True it will use a delay of 20 seconds instead. # Do something here.
While the callbacks will have access to the parameters passed to the decorated function, they should be treated as read-only variables.
the decorated function.
- Return type