UPDATE: I have included a functional API, as per Oleg Komarov request, and amended this post accordingly.
I’ve just released a new module called Action::Retry.
Use it when you want to run some code until it succeeds, waiting between two retries.
A simple way to use it is :
And the Object Oriented API:
The purpose of this module is similar to
AnyEvent::Retry. However, it’s highly configurable, more flexible and has
You can specify the code to try, but also a callback that will be executed to check the success or failure of the attempt. There is also a callback to execute code on failure.
The module also supports different sleep strategies ( Constant, Linear, Fibonacci…) and it’s easy to build yours. Strategies can have their options as well.
And the functional API:
Strategies can decide if it’s worthwhile continuing trying, or if it should fail.
Action::Retry also supports a pseudo “non-blocking” mode, in which it doesn’t actually sleep, but instead returns immediately, and won’t perform the action code until required time has elapsed. Basicaly it allows to do this:
do_something_else should be very fast, so that the loop goes back
quickly to retrying the
So, please give a try to Action::Retry, and let me know what you think.