U  gK @sDdZdgZddlZddlZddlZgddgdgddddf ddZdS)z9!Contains retry_io() which automates retrying operations.retry_ioNTg?Fc  Cs|dk r |dkr|}|dkr |}|dk r4|dkr4|}|dkr@d}|} t|D]J}z0|dk rv|dt|t|f||WStk r}z||dkr|dk r|jdt|fdd|dk r| rt|dttd | |d }t|d |} t |trF|dk r|| kr| d ||d|| t|fnR|dk r|dd}| ||d||dg|dk r|d t|||t td| n|dk r|jdt|fddt |tr | s |dk r|jd||dt|fddnz|dk r|dd}| ||d||dg|dk rT|dt|t |tr|| r|dk r||n||W5d}~XYqLXqLdS)a>!This function automates retrying an unreliable operation several times until it succeeds. This subroutine will retry the operation up to a maximum number of times. If the operation fails too many times, then the last exception thrown by the operation is passed on (raised) to the caller. @param max_tries Maximum number of times to attempt the operation (mandatory) @param sleep_time Time to sleep between tries @param operation A function or callable object that may thrown an Exception @param opargs A list containing arguments to the operation @param logger A logging.Logger object to use for logging, or None to disable logging. @param fail A string to print, or a function to call, when the operation fails but more retries are possible @param failargs Optional: a list of arguments to fail, or None to disable @param giveup A string to print, or a function to call when the operation fails too many times, causing retry_io to give up. Default: same as fail @param giveupargs Optional: a list of arguments to giveup, or None to disable @param randsleep Set to True (default) to enable an exponential backoff algorithm, which will increase the sleep time between tries @param backoff The exponent for the exponential backoff algorithm @param first_warn The first failure at which to warn via the logger @param giveup_quiet If True, a WARNING-level message is sent to the logger if the operation fails more than max_tries times. @return The return value of the operation. @note If fail or giveup are functions, they are passed the contents of failargs (default: opargs) or giveupargs (default: failargs or opargs) with several additional arguments appended. Those arguments are the exception that was caught, the number of attempts so far, the max_tries, the sleep_time, and then a boolean that is true iff the operation is about to be retried.Ng?z%s(%s)z Failed but have not given up: %sT)exc_infog?g?gI@g@z(%s (try %d/%d; sleep %.3f and retry): %szarglist to fail (1): zFailed and gave up: %sz!%s (giving up after %d tries): %sFzarglist to fail (2): )rangedebugrepr Exceptionstrminmaxrandomuniform isinstanceinfoextendtimesleepwarning) max_tries sleep_timeZ operationZopargsloggerfailZfailargsZgiveupZ giveupargsZ randsleepZbackoff first_warn giveup_quietZsleepmeZntryesleepmaxZarglistr;/lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/produtil/retry.pyr sz%            )__doc____all__rloggingr rrrrrs