Download the PHP package prismamedia/php-endeavor without Composer
On this page you can find all versions of the php package prismamedia/php-endeavor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download prismamedia/php-endeavor
More information about prismamedia/php-endeavor
Files in prismamedia/php-endeavor
Package php-endeavor
Short Description PHP utility to retry anything, using various strategies
License BSD-3-Clause
Informations about the package php-endeavor
Endeavor (PHP)
Endeavor is a PHP utility to retry anything, using various strategies.
Requirements
- PHP 7.4+
Installation
Using Composer:
Usage
Simply wrap the code you want to retry in a Closure
in the run()
method:
By default, Endeavor will try to run the given code 5 times. If the first attempt is successful, Endeavor will stop. But if the code fails at each attempt, Endeavor will throw an exception on the last attempt.
The maximum number of attempts can be specified using the second argument of the constructor:
In this example, it will run the code 3 times and throw on the third attempt if it continues to fail.
Keep in mind that the maximum number of attempts includes the first.
A maximum delay can be specified to create a ceiling using the third argument of the constructor:
In this example, the code will be executed 5 times with an exponential strategy which doubles the delay at each attempt to a maximum of 5 seconds.
Strategies
Endeavor can be instantiated with various strategies, depending on the expected retry interval.
Each strategy takes a required delay
in milliseconds and compute the next attempt delay based on
the nature of the strategy.
ConstantStrategy
This is the simplest strategy. It takes a fixed delay and sets the interval between attempt to that number.
LinearStrategy
This strategy takes an initial delay and adds it up at each attempt.
ExponentialStrategy
This strategy takes an initial delay and doubles it at each attempt.
MultiplicativeStrategy
This strategy takes an initial delay and a multiplier then multiplies the delay at each attempt.
Error handling
By default, Endeavor will simply retry the code when an Exception is thrown, then throw the last encountered Exception when reaching the maximum number of attempts.
An error handler can be specified using a Closure
which will be executed after each unsuccessful attempt.
It can be used for logging purpose:
Or even throwing another Exception and stopping Endeavor if the error is unrecoverable:
It can also be used to change the current strategy:
Testing
Testing a class which uses Endeavor can dramatically slow down the execution of tests.
Symfony context
On a Symfony project, this can be resolved using the symfony/phpunit-bridge
package and the included ClockMock
.
_See the documentation on how to setup the bridge and use the @group time-sensitive
annotation._
Finally, on the tests/bootstrap.php
(documentation), register the Endeavor
class:
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.