Retry

Available versions:

Installation

composer require aeon-php/retry

Description

Retry operations that might fail like for example http requests, with different delay modifiers.

<?php

use Aeon\Calendar\TimeUnit;
use Aeon\Retry\Execution;
use function Aeon\Retry\retry;

$result = retry(function (Execution $execution) {
    $random = \random_int(1, 3);

    if ($random === 2) {
        throw new \RuntimeException('exception');
    }

    return $random;
}, 3, TimeUnit::seconds(3));

Object implementation that multiplies delays by retry number after each failure.

<?php

use Aeon\Calendar\System\SystemProcess;use Aeon\Calendar\TimeUnit;
use Aeon\Retry\DelayModifier\RetryMultiplyDelay;use Aeon\Retry\Execution;
use Aeon\Retry\Retry;

return (new Retry(
        SystemProcess::current(),
        5,
        TimeUnit::milliseconds(100)
    ))->modifyDelay(
        new RetryMultiplyDelay()
    )->execute(function (Execution $execution) {
        $random = \random_int(1, 3);

        if ($random === 2) {
            throw new \RuntimeException('exception');
        }

        return $random;
    });