invl / retry

easy to use retry decorator in python
Other
709 stars 114 forks source link

Feature Request - ability to ignore failures from long ago for long running function #37

Open mdavis-xyz opened 4 years ago

mdavis-xyz commented 4 years ago

Use Case

I have a function which will set up a websocket connection to a server. This connection should run forever.

Sometimes websockets break, and a simple retry will get them up again. So I want a retry decorator.

But if it doesn't come up again, and each attempt to reconnect immediately fails, I want to eventually give up, and fail the overall script.

Lets say I set tries=5. At 10am the websocket went down, and after 4 failed attempts to retry, it succeeded on the 5th (final) retry. Now the websocket is fine and runs until 5pm, when it disconnects. This was all one function call, inside the function, inside the decorator for many hours. At 5pm, I want the decorator to retry. But the total retry count is already at 4 (from 10am), so it won't retry at 5pm. I want to tell the decorator to reset the tries count if it hasn't seen an exception after a certain time period.

Solution

Add an extra argument to the decorator, ignore_older_than, which is a number of seconds. e.g. in the case above, maybe set ignore_older_than=60*5 to reset the count for any errors older than 5 minutes.

The way this would interact with backoff is that the sleep between attempts equals delay * (backoff^n) where n is the number of exceptions caught in the last ignore_older_than seconds.

PR

I'm happy to write this PR myself. I just want to check that this is something you would accept.

hudcap commented 2 years ago

I came here for exactly this, but it looks like this package is no longer being maintained...