Download the PHP package gousto/replay without Composer
On this page you can find all versions of the php package gousto/replay. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gousto/replay
More information about gousto/replay
Files in gousto/replay
Package replay
Short Description Replay comes in allowing you to plan re-tries and fallbacks strategies for your functions.
License MIT
Informations about the package replay
Replay
Re-play functions that error out!
Some times things not always works at the first attempt. Replay comes in allowing you to plan re-tries and fallbacks strategies for your functions.
Install
Features
- Re-try functions within exception scope
- Increase delay for every attempt
- Create advanced re-try fallback strategies
Usage
If you want to replay a function when it throws an Exception here is an example:
This code will return you right away the result of Http::get(), if no Exception is occured.
In case of Exception it will retry again up to 3 times with a 50ms interval.
Increase delay at every attempt:
Let's say we want to have a re-try strategy that want to increase the interval at every attempt
i.e: 1st attempt 10ms, 2nd attempts 20ms 3rd attempt 40ms
Custom Exceptions
Replay allow you to trigger the retry logic only for particular Exceptions, pass an array of Exceptions class names as 4th paramter:
This will throw a LogicException when the function is invoked.
We are going to retry only when the RuntimeException is thrown.
API
- retry($attempts = 1, Closure $user_function, $delay = 0, $exception_targets = [Exception::class])
Return the result of the Closure if no exception is occured. Alternatevely it retries invoking the
function until the number of attempts specified. Throw Gousto\Replay\RetryLogicException::class if the number
of attempts remained is 0.
$attempts: int, number of attempts the function might be replayed$user_function:Closure, function which will be replayed if errored$delay:int, delay is measured in milliseconds$exception_targets:array, list of exceptions which will be considered as errors
- times($attempts = 1, Closure $user_function, $delay = 0, $exception_targets = [])
Helper function for instantiate Replay, facilitate chaining
$attempts: int, number of attempts the function might be replayed$user_function:Closure, function which will be replayed if errored$delay:int, delay is measured in milliseconds$exception_targets:array, list of exceptions which will be considered as errors
play($silent = false):
Tell replay to invoke the function and use the retry logic if needed.
$silent:bool, if true will not throwRetryLogicExceptionbut will return it.
onRetry(Closure $handler):
Hook a function that will be trigger for every attempt, this is useful for logging or any other action.
next():
The next function allow you to manually control the retry cycle
attempts():
Return the number of retry attempts
isErrored():
Return the state of the function invokation
Credits
www.gousto.co.uk