PHP class for retrying code pieces in case of exceptions.
Born out of a need of Tobias Schultze and myself for a project at Liip which then found a specific merge request into Doctrine. But we thought this works just as well on a generic basis, hence why I built this. Most code is Tobias, I just wrapped it up into this digestible package.
Make sure it's autoloaded. Wrap the code you want retried with the class and execute it.
use ChristianRiesen\Retry\Retry;
// Anon function
$retry = new Retry(function () { return 42; });
// Outputs 42
echo $retry();
You can configure the exceptions to listen to for retries, the number of retries and the time between retries (in milliseconds) at construction time of the Retry class.
By default it will catch all exceptions
To run tests, install composer, run composer install
(or wherever you have composer installed) then run vendor/phpunit/phpunit/phpunit