Giter VIP home page Giter VIP logo

retry's Introduction

Retry

PHP library for retrying code, e.g. HTTP requests or database transactions, in case of failures.

Build Status

Installation

$ composer require tobion/retry

Usage

use Tobion\Retry\Retry;

$callableThatMightFail = function (int $arg1, int $arg2): int {
    if (random_int(1, 2) % 2) {
        throw new \RuntimeException('Sudden error');
    }

    return $arg1 + $arg2;
};

// Allows you to call the callable with parameters and retry its execution in case an exception is thrown.
// You can access the return value of the callable (3 in this case).
$returnValue = Retry::configure()->call($callableThatMightFail, 1, 2);

// By default:
// - The callable is retried twice (i.e. max three executions). If it still fails, the last error is rethrown.
// - Retries have a no delay between them.
// - Every \Throwable will trigger the retry logic, i.e. both \Exception and \Error.
// You can adjust the retry logic like this:
$retryingCallable = Retry::configure()
    ->maxRetries(5)
    ->delayInMs(100)
    ->retryOnSpecificExceptions(\RuntimeException::class) // other failures like \TypeError will not be retried
    ->decorate($callableThatMightFail)
;
$returnValue = $retryingCallable(1, 2);
// $retryingCallable just decorates the original callable and can be used like it.
// To find out how often it had to retry, you can use:
$retryingCallable->getRetries();

Contributing

To run tests:

$ composer install
$ vendor/bin/simple-phpunit

retry's People

Contributors

christianriesen avatar tobion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.