From 23cc33e28d3f818c6146f291f0c88afa7b5ae862 Mon Sep 17 00:00:00 2001 From: Omar Shaban Date: Thu, 15 Aug 2019 13:53:48 +0200 Subject: [PATCH 1/2] add example to readme --- README.md | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4419073..9e7cd41 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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), From 24fe3efbff8bff0ff5bc456a167e3c1aa7f62e8e Mon Sep 17 00:00:00 2001 From: Omar Shaban Date: Thu, 15 Aug 2019 14:02:27 +0200 Subject: [PATCH 2/2] add maxTriesExhausted to runner --- src/RunnerInterface.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/RunnerInterface.php b/src/RunnerInterface.php index 0874eae..4421594 100644 --- a/src/RunnerInterface.php +++ b/src/RunnerInterface.php @@ -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; }