keboola/retry

Library for repeatable and retryable operations

0.5.0 2020-01-31 14:20 UTC

README

The library for repeatable and retryable operations.
(Forked from https://github.com/vkartaviy/retry)

Build Status

Here is a simple example:

<?php

use Retry\RetryProxy;
use Retry\Policy\SimpleRetryPolicy;
use Retry\BackOff\ExponentialBackOffPolicy;

$retryPolicy = new SimpleRetryPolicy(3);
$backOffPolicy = new ExponentialBackOffPolicy();

$proxy = new RetryProxy($retryPolicy, $backOffPolicy);
$result = $proxy->call(function() {
    // call external service and return result
});

If you want to supply your own retry decider method you can by using the CallableRetryPolicy

<?php

use Retry\RetryProxy;
use Retry\Policy\SimpleRetryPolicy;
use Retry\BackOff\ExponentialBackOffPolicy;

$retryPolicy = new CallableRetryPolicy(function (\Throwable $e) {
    if ($e->getCode() === 200) {
        return false;
    } 
    return true;
});
$proxy = new RetryProxy($retryPolicy, new ExponentialBackOffPolicy());
$result = $proxy->call(function() {
   // call external service and return result
});