s-patompong / php-retrier
PHP Retrier lets you retry your logic easily.
Fund package maintenance!
s-patompong
Requires
- php: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^9.5
- spatie/ray: ^1.28
- vimeo/psalm: ^4.8
README
Retrier can help you retry your logic easily.
<?php // Your own API class use App\Api\ApiConnector; use SPatompong\Retrier\Retrier; $api = new ApiConnector(); // By default, Retrier use RetryThrowableStrategy which will retry if the result is an instance of \Throwable $result = (new Retrier()) ->setLogic(function() use($api) { return $api->get(); }) ->execute();
Installation
You can install the package via composer:
composer require s-patompong/php-retrier
Usage
Fields and their default values:
Minimal configuration example:
<?php // Your own API class use App\Api\ApiConnector; use SPatompong\Retrier\Retrier; $api = new ApiConnector(); $retrier = (new Retrier()) ->setLogic(function() { return 0; }); // After 3 retries, it's possible that the code still gets the \Throwable // Thus, we still need to put it in a try/catch block try { $value = $retrier->execute(); } catch(\Throwable $t) { echo "Still gets throwable after 3 retries.\n"; }
Full configuration example:
<?php // Your own API class use App\Api\ApiConnector; use SPatompong\Retrier\Retrier; use SPatompong\Retrier\Presets\Strategies\RetryNullStrategy; $api = new ApiConnector(); // Keep track of retry count, useful for logging or echoing to the terminal $retryCount = 0; $value = (new Retrier()) // Change the stragegy to RetryNullStrategy to keep retrying if the Logic returns null ->setRetryStrategy(new RetryNullStrategy()) // Set the wait time for each retry to 10 seconds ->setDelay(10) // Let the code retry 5 times ->setRetryTimes(5) // Set the onRetryListener to print out some useful log ->setOnRetryListener(function ($currentTryCount, $value, $throwable) use (&$retryCount) { $retryCount++; echo "Failed to get API data, retry count: $retryCount\n"; }) // Set the logic ->setLogic(fn () => $api->get()) // Execute it ->execute(); // At this point, value could still be null if after 5 times the code still couldn't get the API data echo $value;
It's also possible to use callable array syntax when set the logic or retryListener:
<?php use SPatompong\Retrier\Retrier; use SPatompong\Retrier\Tests\Helpers\FakeClass; $fakeClass = new FakeClass(); $publicMethodResult = (new Retrier()) ->setLogic([$fakeClass, 'fakePublicMethod']) ->execute(); $staticMethodResult = (new Retrier()) ->setLogic([FakeClass::class, 'fakeStaticMethod']) ->execute();
Retry Strategy
RetryStrategy is a class that implement RetryStrategy interface. The Retrier class uses it to determine if it should retry or not (given the return value from the logic).
<?php namespace SPatompong\Retrier\Contracts; interface RetryStrategy { /** * Add a logic to check if the retrier should retry * * @param mixed $value * @return bool */ public function shouldRetry(mixed $value): bool; }
This library provides two presets strategy:
RetryThrowableStrategy
- This is a default strategy that will retry any \Throwable response.RetryNullStrategy
- This strategy will keep retry if the response is NULL.
If you want to have a custom shouldRetry()
logic, you can create your own RetryStrategy class and implement this RetryStrategy interface.
<?php namespace App\RetryStrategies; use SPatompong\Retrier\Contracts\RetryStrategy; use GuzzleHttp\Exception\ClientException; class RetryGuzzleClientExceptionStrategy implements RetryStrategy { public function shouldRetry(mixed $value): bool { return $value instanceof ClientException; } }
Then, set it as a retry strategy of the Retrier:
<?php use SPatompong\Retrier\Retrier; use App\RetryStrategies\RetryGuzzleClientExceptionStrategy; $retrier = (new Retrier()) ->setRetryStrategy(new RetryGuzzleClientExceptionStrategy()) try { $retrier->execute(); } catch(\Throwable $t) { // Still gets ClientException after retry or other type of Throwable }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.