zarganwar / failure-retry-executor
Retry failed operations
Installs: 242
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/zarganwar/failure-retry-executor
Requires
- php: >=8.1
README
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, );