PHP code example of jbzoo / retry

1. Go to this page and download the library: Download jbzoo/retry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jbzoo / retry example snippets


use function JBZoo\Retry\retry;

$result = retry(function() {
    return doSomeWorkThatMightFail();
});

use JBZoo\Retry\Retry;

$retry = new Retry(10, 'exponential', 10000, true);
$result = $retry->run(function() {
    return doSomeWorkThatMightFail();
});

use JBZoo\Retry\Retry;

// Assuming a fresh instance of $retry was handed to you
$result = (new Retry())
    ->setStrategy('constant')
    ->setMaxAttempts(10)
    ->enableJitter()
    ->run(function() {
        return doSomeWorkThatMightFail();
    });

use JBZoo\Retry\Retry;

Retry::$defaultMaxAttempts;
Retry::$defaultStrategy;
Retry::$defaultJitterEnabled;

use JBZoo\Retry\Strategies\ConstantStrategy;

$strategy = new ConstantStrategy(500);

use JBZoo\Retry\Strategies\LinearStrategy;
$strategy = new LinearStrategy(200);

use JBZoo\Retry\Strategies\PolynomialStrategy;
$strategy = new PolynomialStrategy(100, 3);

use JBZoo\Retry\Strategies\ExponentialStrategy;
$strategy = new ExponentialStrategy(100);

use JBZoo\Retry\Retry;
use function JBZoo\Retry\retry;

retry(function() {
    // ...
}, 10, 'constant');

// OR

$retry = new Retry(10, 'constant');

use JBZoo\Retry\Retry;
use JBZoo\Retry\Strategies\LinearStrategy;
use function JBZoo\Retry\retry;

retry(function() {
    // ...
}, 10, new LinearStrategy(500));

// OR

$retry = new Retry(10, new LinearStrategy(500));

use JBZoo\Retry\Retry;
use function JBZoo\Retry\retry;

retry(function() {
    // ...
}, 10, 1000);

// OR

$retry = new Retry(10, 1000);

use JBZoo\Retry\Retry;
use function JBZoo\Retry\retry;

retry(function() {
    // ...
}, 10, function($attempt) {
    return (100 * $attempt) + 5000;
});

// OR

$retry = new Retry(10);
$retry->setStrategy(function($attempt) {
    return (100 * $attempt) + 5000;
});

use JBZoo\Retry\Retry;

$retry = new Retry();
$retry->setDecider(function($attempt, $maxAttempts, $result, $exception = null) {
    return someCustomLogic();
});

use JBZoo\Retry\Retry;

$retry = new Retry();
$retry->setErrorHandler(function($exception, $attempt, $maxAttempts) {
    Log::error("On run {$attempt}/{$maxAttempts} we hit a problem: {$exception->getMessage()}");
});