diephp / perhaps
Catch and Retry Code Execution
Requires
- php: ^7.4 || ^8.0
- diephp/sequences: ^1.0
- psr/log: ^2.0 || ^3.0
Requires (Dev)
- phpunit/phpunit: ^6.4|^8.5|^9.0
README
Perhaps Retry Library
A Laravel package that provides a simple way to retry any logic with customizable attempts and delays. This can be particularly useful in scenarios where certain operations might fail due to transient issues, such as network errors or temporary service outages.
Installation
-
Install the package via Composer:
composer require diephp/perhaps
-
Register the service provider in your
config/app.php
file:'providers' => [ // ... \DiePHP\Perhaps\Providers\PerhapsServiceProvider::class, ],
or for laravel 11+ add Provider to list in bootstrap/providers.php
return [ App\Providers\AppServiceProvider::class, ... \DiePHP\Perhaps\Providers\PerhapsServiceProvider::class, ];
-
Optionally, publish the package configuration:
php artisan vendor:publish --tag=perhaps
Usage
Basic Usage
The main function provided by this package is Perhaps::retry
. You can use it to retry any logic with a specified number of attempts.
use DiePHP\Perhaps\Facades\Perhaps; Perhaps::retry(function() { // Your logic here }, 3); // Retry 3 times
Handling Delays
You can also specify a delay sequence using a Traversable
iterator. This allows you to define custom delay logic between retries.
you can see details on https://github.com/diephp/sequences
use DiePHP\Perhaps\Facades\Perhaps; use DiePHP\Sequences\ProgressiveSequence; Perhaps::retry(function() { // Your logic here }, 10, new ProgressiveSequence(1000000, 100)); // Retry 10 times with progressive delay
use DiePHP\Perhaps\Facades\Perhaps; use DiePHP\Sequences\RandSequence; Perhaps::retry(function() { // Your logic here }, 10, new RandSequence(1000000, 90000000));
use DiePHP\Perhaps\Facades\Perhaps; use DiePHP\Sequences\ExponentialSequence; Perhaps::retry(function() { // Your logic here }, 1000, new ExponentialSequence(10, 100));
Example with Exception Handling
In this example, the retry logic will handle exceptions and log the retry attempts.
Perhaps::retry(function($attempt) { if ($attempt < 3) { throw new \Exception("Simulated failure"); } // Successful operation echo "Success on attempt $attempt"; }, 5); // Retry 5 times
Configuration
You can configure the package by modifying the published configuration file config/perhaps.php
. This allows you to customize the logging and exception handling behavior.
License
This package is open-source software licensed under the MIT license.
Key Points
- The
retry
method allows you to retry any callable logic a specified number of times. - You can specify a delay sequence for custom delay logic between retries.
- Register the service provider and optionally publish the configuration for customization.
- Use the provided facade for easy access to the
retry
method in your Laravel application.