|
1 | | -# retry |
| 1 | +Retry |
| 2 | +===== |
2 | 3 |
|
3 | | -A tiny library for retrying failing operations. |
| 4 | +[![Latest version][Version image]][Releases] |
| 5 | +[![Build status][Build image]][Build] |
| 6 | +[![Test coverage][Coverage image]][Coverage] |
| 7 | +[![Code style][Style image]][Style] |
4 | 8 |
|
5 | | -Since the network is reliable, things should always work. Am I right? For those cases when they don't, there is *retry*. |
| 9 | +Retry provides a function to retry failing operations. An operation is deemed to have failed if it throws an exception. |
6 | 10 |
|
7 | | -```php |
8 | | -<? |
9 | | -use function igorw\retry; |
| 11 | +Requirements |
| 12 | +------------ |
10 | 13 |
|
11 | | -// retry an operation up to 5 times |
12 | | -$user = retry(5, function () use ($id) { |
13 | | - return User::find($id); |
14 | | -}); |
| 14 | +- [PHP 5.5](http://php.net/) |
| 15 | +- [Composer](https://getcomposer.org/) |
| 16 | + |
| 17 | +Usage |
| 18 | +----- |
| 19 | + |
| 20 | +The `retry` function has the following signature. |
| 21 | + |
| 22 | +``` |
| 23 | +retry(int $times, callable $operation); |
| 24 | +``` |
| 25 | +* `$times` specifies how many times the operation may be called. |
| 26 | +* `$operation` is a callback to be run up to the specified number of times. |
| 27 | + |
| 28 | +Note that in the [original library](https://github.com/igorw/retry), `$times` specified the number of *retries* and therefore the operation could run up to `n + 1` times. In this version, `$times` specifies exactly the number of times the operation may run such that if zero (`0`) is specified it will never run. |
| 29 | + |
| 30 | +### Example |
15 | 31 |
|
16 | | -// here is why you want to start using HHVM |
17 | | -$user = retry(5, () ==> User::find($id)); |
| 32 | +```php |
| 33 | +use function ScriptFUSION\Retry\retry; |
18 | 34 |
|
19 | | -// this is probably a bad idea |
20 | | -$user = retry(INF, () ==> { |
21 | | - throw new RuntimeException('never gonna give you up'); |
| 35 | +// Try an operation up to 5 times. |
| 36 | +$response = retry(5, function () use ($url) { |
| 37 | + return HttpConnector::fetch($url); |
22 | 38 | }); |
23 | | -?> |
24 | 39 | ``` |
25 | 40 |
|
26 | | -I know. You're welcome. |
| 41 | + |
| 42 | + [Releases]: https://github.com/ScriptFUSION/Retry/releases |
| 43 | + [Version image]: https://poser.pugx.org/scriptfusion/retry/v/stable "Latest version" |
| 44 | + [Build]: http://travis-ci.org/ScriptFUSION/Retry |
| 45 | + [Build image]: https://travis-ci.org/ScriptFUSION/Retry.svg "Build status" |
| 46 | + [Coverage]: https://coveralls.io/github/ScriptFUSION/Retry |
| 47 | + [Coverage image]: https://coveralls.io/repos/ScriptFUSION/Retry/badge.svg "Test coverage" |
| 48 | + [Style]: https://styleci.io/repos/62990558 |
| 49 | + [Style image]: https://styleci.io/repos/62990558/shield?style=flat "Code style" |
| 50 | + |
0 commit comments