Skip to content

ScriptFUSION/Retry

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code
This branch is 14 commits ahead of igorw:master.

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 

Retry

Latest version Total downloads Build status Test coverage Code style

Retry provides a function to retry failing operations with optional Fiber support. An operation is deemed to have failed if it throws an exception. This library is a rewrite of Igor Wiedler's retry but aims to remain faithful to the spirit of the original.

Requirements

Usage

The retry function retries an operation up to the specified number of times with an optional exception handler.

If an exception handler is specified, it is called immediately before retrying the operation. If the handler returns false, the operation is not retried.

retry(int $times, callable $operation, callable $onError = null);
  • $times—Maximum number of times the operation may run.
  • $operation—Operation to run up to the specified number of times.
  • $onError—Optional. Exception handler that receives the thrown exception as its first argument.

Note in the original library, $times specifies the number of retries and therefore the operation could run up to $times + 1 times. In this version, $times specifies exactly the number of times the operation may run such that if zero (0) is specified it will not run.

Example

The following code fragment attempts to fetch data from a URL over HTTP up to five times.

use function ScriptFUSION\Retry\retry;

$response = retry(5, function () use ($url) {
    return HttpConnector::fetch($url);
});

About

🔃 Retries failing operations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%