Package ganeti :: Package utils :: Module retry
[hide private]
[frames] | no frames]

Module retry

source code

Utility functions for retrying function calls with a timeout.

Classes [hide private]
  RetryTimeout
Retry loop timed out.
  RetryAgain
Retry again.
  _RetryDelayCalculator
Calculator for increasing delays.
Functions [hide private]
 
Retry(fn, delay, timeout, args=None, wait_fn=time.sleep, _time_fn=time.time)
Call a function repeatedly until it succeeds.
source code
 
SimpleRetry(expected, fn, delay, timeout, args=None, wait_fn=time.sleep, _time_fn=time.time)
A wrapper over Retry implementing a simpler interface.
source code
 
CountRetry(expected, fn, count, args=None)
A wrapper over SimpleRetry implementing a count down.
source code
 
RetryByNumberOfTimes(max_retries, exception_class, fn, *args, **kwargs)
Retries calling a function up to the specified number of times.
source code
Variables [hide private]
  RETRY_REMAINING_TIME = object()
Special delay to specify whole remaining timeout

Imports: logging, time, errors


Function Details [hide private]

Retry(fn, delay, timeout, args=None, wait_fn=time.sleep, _time_fn=time.time)

source code 

Call a function repeatedly until it succeeds.

The function fn is called repeatedly until it doesn't throw RetryAgain anymore. Between calls a delay, specified by delay, is inserted. After a total of timeout seconds, this function throws RetryTimeout.

delay can be one of the following:

  • callable returning the delay length as a float
  • Tuple of (start, factor, limit)
  • RETRY_REMAINING_TIME to sleep until the timeout expires (this is useful when overriding wait_fn to wait for an external event)
  • A static delay as a number (int or float)
Parameters:
  • fn (callable) - Function to be called
  • delay - Either a callable (returning the delay), a tuple of (start, factor, limit) (see _RetryDelayCalculator), RETRY_REMAINING_TIME or a number (int or float)
  • timeout (float) - Total timeout
  • wait_fn (callable) - Waiting function
Returns:
Return value of function

SimpleRetry(expected, fn, delay, timeout, args=None, wait_fn=time.sleep, _time_fn=time.time)

source code 

A wrapper over Retry implementing a simpler interface.

All the parameters are the same as for Retry, except it has one extra argument: expected, which can be either a value (will be compared with the result of the function, or a callable (which will get the result passed and has to return a boolean). If the test is false, we will retry until either the timeout has passed or the tests succeeds. In both cases, the last result from calling the function will be returned.

Note that this function is not expected to raise any retry-related exceptions, always simply returning values. As such, the function is designed to allow easy wrapping of code that doesn't use retry at all (e.g. "if fn(args)" replaced with "if SimpleRetry(True, fn, ...)".

See Also: Retry

CountRetry(expected, fn, count, args=None)

source code 

A wrapper over SimpleRetry implementing a count down.

Where Retry fixes the time, after which the command is assumed to be failing, this function assumes the total number of tries.

See Also: Retry

RetryByNumberOfTimes(max_retries, exception_class, fn, *args, **kwargs)

source code 

Retries calling a function up to the specified number of times.

Parameters:
  • max_retries (integer) - Maximum number of retries.
  • exception_class (class) - Exception class which is used for throwing the final exception.
  • fn (callable) - Function to be called (up to the specified maximum number of retries.