Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Zarganwar/failure-retry-executor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

failure-retry-executor

Usage

Simply run function Zarganwar\FailureRetryExecutor\FailureRetryExecutor::execute() with your command as first argument

Zarganwar\FailureRetryExecutor\FailureRetryExecutor::execute(fn() => someFunction());

Your function will be executed and if it fails (throws an Exception or returns false), it will be executed again. Max. default retries is 3. Of course, you can change it by passing maxAttempts: int argument.

Zarganwar\FailureRetryExecutor\FailureRetryExecutor::execute(
    command: fn() => someFunction(),
    maxAttempts: 99,
);

You can also respond to the command result or failure by passing onSuccess: callable or/and onFailure: callable arguments.

Zarganwar\FailureRetryExecutor\FailureRetryExecutor::execute(
    command: fn() => someFunction(),
    onSuccess: fn($result /* Your callable command result */) => doSomething($result),
    onFailure: fn(Throwable $throwable) => log($throwable),
);

Example of usage:

// Some HTTP client
$client = new Client();
$logger = new Logger();

Zarganwar\FailureRetryExecutor\FailureRetryExecutor::execute(
    command: fn() => $client->get('https://example.com'),
    onSuccess: function(ResponseInterface $response): void 
    {       
        if ($response->getStatusCode() !== 200) {
            throw new Exception("Server responded with status code {$response->getStatusCode()} instead of 200");
        }
    },
    onFailure: fn(Throwable $throwable) => $logger->log($throwable),
    maxAttempts: 5,
);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages