netlogix/retry

Retries a specific PHP code fragment.

1.0.0 2025-08-05 12:55 UTC

This package is auto-updated.

Last update: 2025-08-05 14:18:11 UTC


README

Retries a specific PHP code fragment (called a "task") as long as the "when" condition evaluates to true.

(new \Netlogix\Retry\Retry())
    ->when(condition: fn(int $incarnation) => $incarnation < PHP_INT_MAX)
    ->task(subject: fn () => throw new \RuntimeException('false'));

There is a shorthand method for counting retries and sleeping between attempts with increasing intervals.

(new \Netlogix\Retry\Retry())
    /**
     * http://backoffcalculator.com/?attempts=5&rate=1&interval=0.5
     * = (0.5 + 1 + 1.5 + 2.0 + 2.5) seconds
     * = 7.5 seconds
     */
    ->withExponentialBackoff(retryInterval: 0.5, maxRetries: 5)
    ->onExceptionsOfType(\Doctrine\DBAL\Exception\DeadlockException::class)
    ->task(fn () => $dbal->executeQuery($statement));