Skip to content

Commit 00a71cd

Browse files
authored
Merge pull request #2 from Tobion/overengineer
Overengineer + Modernize
2 parents bce8485 + 9a4f976 commit 00a71cd

18 files changed

+639
-362
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/tests export-ignore
2+
.editorconfig export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore
5+
.travis.yml export-ignore
6+
phpunit.xml.dist export-ignore

.gitignore

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Composer
2-
/composer.phar
3-
/composer.lock
4-
/vendor
5-
6-
/build
1+
composer.lock
2+
phpunit.xml
3+
vendor/

.travis.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
7-
- hhvm
8-
- 7
9-
10-
before_install:
11-
- composer self-update
4+
- 7.1
5+
- 7.2
6+
- 7.3
7+
- 7.4snapshot
128

139
install:
14-
- composer install --prefer-source
10+
- composer install --no-interaction --prefer-dist
1511

1612
script:
1713
- ./vendor/bin/phpunit --coverage-text

LICENSE

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The MIT License (MIT)
1+
MIT License
22

3-
Copyright (c) 2015 Tobias Schultze and Christian Riesen
3+
Copyright (c) 2015-2019 Tobias Schultze
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22-

README.md

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
11
Retry
22
=====
33

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.
55

66
[![Build Status](https://travis-ci.org/Tobion/retry.svg)](https://travis-ci.org/Tobion/retry)
77

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
1112

1213
Usage
1314
-----
1415

15-
Make sure it's autoloaded. Wrap the code you want retried with the class and execute it.
16-
1716
```php
1817
use Tobion\Retry\Retry;
1918

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();
2646
```
2747

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+
------------
3150

32-
Development
33-
-----------
51+
To run tests:
3452

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

composer.json

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,20 @@
22
"name": "tobion/retry",
33
"type": "library",
44
"description": "A generic library to retry an operation in case of an error. You can configure the behavior like the exceptions to retry on.",
5-
"keywords": [
6-
"retry",
7-
"retryable",
8-
"restart",
9-
"repeat"
10-
],
5+
"keywords": ["retry", "retryable", "restart", "repeat"],
116
"homepage": "https://github.com/Tobion/retry",
127
"license": "MIT",
138
"authors": [
149
{
1510
"name": "Tobias Schultze",
16-
"role": "Developer"
17-
},
18-
{
19-
"name": "Christian Riesen",
20-
"email": "chris.riesen@gmail.com",
21-
"homepage": "http://christianriesen.com",
22-
"role": "Developer"
11+
"homepage": "https://github.com/Tobion"
2312
}
2413
],
2514
"require": {
26-
"php": ">=5.4"
15+
"php": ">=7.1"
2716
},
2817
"require-dev": {
29-
"phpunit/phpunit": "4.*"
18+
"phpunit/phpunit": "^7.5"
3019
},
3120
"autoload": {
3221
"psr-4": {
@@ -40,7 +29,7 @@
4029
},
4130
"extra": {
4231
"branch-alias": {
43-
"dev-master": "0.1.x-dev"
32+
"dev-master": "1.x-dev"
4433
}
4534
}
4635
}

phpunit.xml.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.6/phpunit.xsd"
5-
backupGlobals = "false"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
65
beStrictAboutTestsThatDoNotTestAnything = "true"
76
beStrictAboutOutputDuringTests = "true"
87
colors = "true"

src/DelayMilliseconds.php renamed to src/ExceptionHandler/DelayMilliseconds.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
<?php
22

3-
namespace Tobion\Retry;
3+
namespace Tobion\Retry\ExceptionHandler;
44

55
/**
66
* Invokable class that delays execution by given number of milliseconds with usleep.
77
*
88
* @author Tobias Schultze <http://tobion.de>
99
* @author Christian Riesen <http://christianriesen.com>
1010
*/
11-
class DelayMilliseconds
11+
final class DelayMilliseconds
1212
{
1313
/**
14-
* Delay in milliseconds.
15-
*
1614
* @var int
1715
*/
1816
private $milliseconds;
1917

20-
/**
21-
* Constructor.
22-
*
23-
* @param int $milliseconds Delay in milliseconds
24-
*/
25-
public function __construct($milliseconds)
18+
public function __construct(int $milliseconds)
2619
{
2720
$this->milliseconds = $milliseconds;
2821
}
2922

30-
/**
31-
* Wait the configured amount of milliseconds.
32-
*
33-
* @return void
34-
*/
35-
public function __invoke()
23+
public function __invoke(): void
3624
{
3725
if ($this->milliseconds <= 0) {
3826
return;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tobion\Retry\ExceptionHandler;
4+
5+
/**
6+
* Invokable class that delegates exception handling to an array of callables.
7+
*
8+
* @author Tobias Schultze <http://tobion.de>
9+
*/
10+
final class DelegatingStack
11+
{
12+
/**
13+
* @var callable[]
14+
*/
15+
private $exceptionHandlers = [];
16+
17+
public function __construct(callable ...$exceptionHandlers)
18+
{
19+
$this->exceptionHandlers = $exceptionHandlers;
20+
}
21+
22+
public function __invoke(\Throwable $e): void
23+
{
24+
foreach ($this->exceptionHandlers as $callable) {
25+
$callable($e);
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)