PHP code example of koschos / php-retry

1. Go to this page and download the library: Download koschos/php-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/ */

    

koschos / php-retry example snippets


// Build retry template with 5 retries and 100 milliseconds waits between them
$retryTemplate = RetryTemplateBuilder::getBuilder()
    ->withMaxAttempts(5)
    ->withBackOffPeriod(100)
    ->build();

// And run your code placed inside callback
$result = $retryTemplate->execute(new class implements RetryCallback {
  public function doWithRetry(RetryContext $context) {
      // dangerous code
  }
});

interface RetryOperations
{
    public function execute(RetryCallback $retryCallback);

    public function executeWithRecovery(RetryCallback $retryCallback, RecoveryCallback $recoveryCallback);
}

interface RetryCallback
{
    public function doWithRetry(RetryContext $retryContext);
}

$template = new RetryTemplate();

// Retry policy with 30 seconds timeout
$policy = new TimeoutRetryPolicy();
$policy->setTimeout(30000);
$template->setRetryPolicy($policy);

$result = $template->execute($myRetryCallback);

$template->executeWithRecovery($myRetryCallback, $myRecoveryCallback);

interface RetryPolicy
{
    public function canRetry(RetryContext $context);

    public function open();

    public function close(RetryContext $context);

    public function registerException(RetryContext $context, \Exception $exception);
}

$policy = new SimpleRetryPolicy(5, [\Exception.class, true]);

interface BackOffPolicy
{
    public function start(RetryContext $context);

    public function backOff(RetryContext $context);
}
bash
composer