Skip to content

Commit

Permalink
Merge pull request #2 from eshta/readme-example
Browse files Browse the repository at this point in the history
add example to readme
  • Loading branch information
0mars authored Aug 15, 2019
2 parents 3a876fd + 24fe3ef commit 81c198f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![PHPv](https://img.shields.io/packagist/php-v/eshta/resilient-task.svg)](http://www.php.net)
[![Downloads](https://img.shields.io/packagist/dt/eshta/resilient-task.svg)](https://packagist.org/packages/eshta/resilient-task)

TODO: Project description
Resilient Task Runner, A circuit breaker implementation, highly configurable task runner with number of max retries, back-off factor, maximum sleep time, and starting sleep time.

## Usage

Expand All @@ -18,13 +18,32 @@ $ composer require eshta/resilient-task

## Example
```php
$procedure = function() use (&$executionTimes) {
$executionTimes++;
return $executionTimes;
use GuzzleHttp\Exception\ConnectException;


$task = function() {
try {
$response = $client->request('GET', 'https://github.com/_abc_123_404');

return $response;
} catch (ConnectException $e) {
echo Psr7\str($e->getRequest());
}
};

$runner = new ResilientTaskRunner(50, 60, 0.5);
$runner = new ResilientTaskRunner(10, 16, 0.5);
$response = $runner->run($task);

if ($runner->maxTriesExhausted()) {
throw new MyFavouriteException('Service call failed!');
}
```
- try 10 times at most
- maximum sleep time between retries 16 seconds
- first sleep time is half a second
- back-off factor [2 default]: double sleeping time after each failed attempt

**Note:**: the runner will only stop when there is a non-null result returned by the task

## Contributing
See [CONTRIBUTING](CONTRIBUTING.md) and [Code of Conduct](CONDUCT.md),
Expand Down
7 changes: 7 additions & 0 deletions src/RunnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ interface RunnerInterface
* @return mixed|null
*/
public function run(callable $task);

/**
* Concludes a failure after exhausting all tries
*
* @return bool
*/
public function maxTriesExhausted(): bool;
}

0 comments on commit 81c198f

Please sign in to comment.