tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

max wait doesn't stop the retries

Open cristianmtr opened this issue 5 years ago • 2 comments

Hey

I have configure my function like so:

@retry(wait=wait_exponential(multiplier=1, max=6),
           stop=retry_if_result(lambda state: isinstance(state, ValueError)),
           retry_error_callback=log_exception,
           after=after_log(logger, logging.WARNING))

However, when the waiting time is exceeded the function is still continuing. What am I doing wrong here?

cristianmtr avatar Aug 05 '20 12:08 cristianmtr

You did not set any condition to stop based on any max time. the max= in wait_exponential is just the maximum wait time between 2 retries.

jd avatar Aug 05 '20 13:08 jd

I finally figured it out that I had to explicitly add a stop condition. This doesn't seem intuitive to me. I would expect the function to return once a non-exception is returned.

This is what worked for me, for reference for future users:

    @retry(
        wait=wait_random_exponential(multiplier=1, max=15),
        stop=stop_after_attempt(7),
        retry=retry_if_not_result(lambda r: isinstance(r, (dict, ValueError))),
        retry_error_callback=lambda retry_state: logger.warning(
            f"Retry state: {retry_state.outcome.result()}"
        ),
        after=after_log(logger, logging.WARNING),
    )

To achieve the following:

 
        - we re-try the function in exponential increments of 2^x * 1, up to max X seconds between attempts
        - we stop after 7 attempts or if we return a dict or a ValueError
        - we log the last result
        - we log the retry attempt #

cristianmtr avatar Aug 05 '20 14:08 cristianmtr