|
1 | 1 | Retry |
2 | 2 | ===== |
3 | 3 |
|
4 | | -PHP class for retrying code pieces in case of exceptions. |
| 4 | +PHP library for retrying code, e.g. HTTP requests or database transactions, in case of failures. |
5 | 5 |
|
6 | 6 | [](https://travis-ci.org/Tobion/retry) |
7 | 7 |
|
8 | | -Born out of a need of [Tobias Schultze](https://github.com/Tobion) and [Christian Riesen](https://github.com/ChristianRiesen) for a project at [Liip](http://www.liip.ch) |
9 | | -which then found a specific [merge request into Doctrine](https://github.com/doctrine/dbal/pull/718/files). We |
10 | | -thought this works just as well on a generic basis. |
| 8 | +Installation |
| 9 | +------------ |
| 10 | + |
| 11 | + $ composer require tobion/retry |
11 | 12 |
|
12 | 13 | Usage |
13 | 14 | ----- |
14 | 15 |
|
15 | | -Make sure it's autoloaded. Wrap the code you want retried with the class and execute it. |
16 | | - |
17 | 16 | ```php |
18 | 17 | use Tobion\Retry\Retry; |
19 | 18 |
|
20 | | -// Anon function |
21 | | -$retry = new Retry(function () { return 42; }); |
22 | | - |
23 | | -// Outputs 42 |
24 | | -echo $retry(); |
25 | | - |
| 19 | +$callableThatMightFail = function (int $arg1, int $arg2): int { |
| 20 | + if (random_int(1, 2) % 2) { |
| 21 | + throw new \RuntimeException('Sudden error'); |
| 22 | + } |
| 23 | + |
| 24 | + return $arg1 + $arg2; |
| 25 | +}; |
| 26 | + |
| 27 | +// Allows you to call the callable with parameters and retry its execution in case an exception is thrown. |
| 28 | +// You can access the return value of the callable (3 in this case). |
| 29 | +$returnValue = Retry::retry($callableThatMightFail, 1, 2); |
| 30 | + |
| 31 | +// By default: |
| 32 | +// - The callable is retried twice (i.e. max three executions). If it still fails, the last error is rethrown. |
| 33 | +// - Retries have a 300 milliseconds delay between them. |
| 34 | +// - Every \Throwable will trigger the retry logic, i.e. both \Exception and \Error. |
| 35 | +// You can adjust the retry logic like this: |
| 36 | +$retryingCallable = Retry::configure() |
| 37 | + ->setMaxRetries(5) |
| 38 | + ->setDelayInMs(100) |
| 39 | + ->setRetryableExceptions(\RuntimeException::class) // other failures like \TypeError will not be retried |
| 40 | + ->decorate($callableThatMightFail) |
| 41 | +; |
| 42 | +$returnValue = $retryingCallable(1, 2); |
| 43 | +// $retryingCallable just decorates the original callable and can be used like it. |
| 44 | +// To find out how often it had to retry, you can use: |
| 45 | +$retryingCallable->getRetries(); |
26 | 46 | ``` |
27 | 47 |
|
28 | | -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. |
29 | | - |
30 | | -By default it will catch all exceptions |
| 48 | +Contributing |
| 49 | +------------ |
31 | 50 |
|
32 | | -Development |
33 | | ------------ |
| 51 | +To run tests: |
34 | 52 |
|
35 | | -To run tests, install [composer](https://getcomposer.org/), run `composer install` (or wherever you have composer installed) then run `vendor/phpunit/phpunit/phpunit` |
| 53 | + $ composer install |
| 54 | + $ vendor/bin/phpunit |
0 commit comments