Download the PHP package fostam/retry without Composer
On this page you can find all versions of the php package fostam/retry. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package retry
fostam/retry
With Retry, you can repeat the execution of any callable until it either succeeds, or the maximum number of attempts has been reached. Several delay policies are available for determining the sleep time between the attempts.
Features
- Simple usage
- Different delay strategies
- Works with any PHP Callable
- No dependencies
Install
The easiest way to install Retry is by using composer:
Usage
`
Payload Callables
Payload callables can be functions, class/object methods or closures, as listed in the PHP documentation.
Failure Condition
Payload callables are considered to have failed if they either throw an Exception
or return false
.
If false
is a legitimate result of your callable, use onException()
instead of onFailure()
,
in which case the return value of the callable will be ignored.
The original exception that was thrown in the payload callable is chained to the RetryLimitException
and can be retrieved via $e->getPrevious()
.
You can also use the onCondition()
method and pass a validation callable that takes the result
of the payload callable as argument and returns true
or false
to indicate whether the
original callable failed or not:
`
Passing Parameters
If you use a function or a class/object method as callable and need to pass parameters, you can wrap the call with a closure:
`
Delay Policies
Retry sleeps between two payload calls. There are multiple policies available to determine the time period of the delay. All numerical values for delay policies are interpreted as milliseconds (ms).
Constant Sleep Value
Linear Sleep Value Increase
Exponential Sleep Value Increase
Fixed Series of Sleep Values
Sleep with Random Jitter
No Sleep
Advanced Features
Omitting Delay Policy
In case you do not want any delay between the attempts, you can either use the
NoneDelayPolicy
as described above, pass NULL
as Delay Policy, or omit the Delay
Policy argument at all:
Getting the Number of Attempts
If you need to get the number of attempts that were necessary for a successful payload call, e.g. for logging purposes,
you can pass a variable by reference to the onFailure()
, onException()
and onCondition()
methods:
Abort the Retry Loop
To abort the retry loop without signalling "success", an AbortException
can be thrown from inside the
payload function.