diff --git a/README.md b/README.md index 8b2b5a1..8a822be 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Resiliency, an implementation for resilient and modern PHP applications -[![codecov](https://codecov.io/gh/loveOSS/resiliency/branch/master/graph/badge.svg)](https://codecov.io/gh/loveOSS/resiliency) [![PHPStan](https://img.shields.io/badge/PHPStan-Level%207-brightgreen.svg?style=flat&logo=php)](https://shields.io/#/) [![Psalm](https://img.shields.io/badge/Psalm-Level%20Max-brightgreen.svg?style=flat&logo=php)](https://shields.io/#/) [![Build Status](https://travis-ci.com/loveOSS/resiliency.svg?branch=master)](https://travis-ci.com/loveOSS/resiliency) +[![codecov](https://codecov.io/gh/loveOSS/resiliency/branch/master/graph/badge.svg)](https://codecov.io/gh/loveOSS/resiliency) [![PHPStan](https://img.shields.io/badge/PHPStan-Level%20max-brightgreen.svg?style=flat&logo=php)](https://shields.io/#/) [![Psalm](https://img.shields.io/badge/Psalm-Level%20Max-brightgreen.svg?style=flat&logo=php)](https://shields.io/#/) [![Build Status](https://travis-ci.com/loveOSS/resiliency.svg?branch=master)](https://travis-ci.com/loveOSS/resiliency) ## Main principles diff --git a/composer.json b/composer.json index b99ca9a..6a68022 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,8 @@ "phpstan/phpstan": "^0.12", "phpstan/phpstan-phpunit": "^0.12", "phpunit/phpunit": "^8.0", - "symfony/cache": "~4.4|~5.3", - "symfony/http-client": "^4.4|~5.3", + "symfony/cache": "~4.4|~5.4|~6.0", + "symfony/http-client": "^4.4|~5.4|~6.0", "vimeo/psalm": "^4.3" }, "suggest": { @@ -49,7 +49,7 @@ }, "scripts": { "cs-fix": "@php ./vendor/bin/php-cs-fixer fix", - "phpqa": "@php ./vendor/bin/phpqa --report --tools phpcs:0,phpmd:0,phpmetrics,phploc,pdepend,security-checker:0,parallel-lint:0 --ignoredDirs tests,vendor", + "phpqa": "@php ./vendor/bin/phpqa --report --tools phpcs:0,phpmetrics,phploc,pdepend,security-checker:0,parallel-lint:0 --ignoredDirs tests,vendor", "phpstan": "@php ./vendor/bin/phpstan analyse src --level max -c extension.neon", "psalm": "@php ./vendor/bin/psalm --threads=8 --diff", "test": "@php ./vendor/bin/phpunit" diff --git a/psalm.xml.dist b/psalm.xml.dist index d393342..25e2624 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -3,7 +3,6 @@ name="Psalm configuration for the Resiliency library" totallyTyped="true" strictBinaryOperands="true" - allowPhpStormGenerics="true" allowStringToStandInForClass="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" diff --git a/src/Contracts/ThrowableEvent.php b/src/Contracts/ThrowableEvent.php new file mode 100644 index 0000000..06f2e68 --- /dev/null +++ b/src/Contracts/ThrowableEvent.php @@ -0,0 +1,14 @@ +exception = $exception; + + parent::__construct($circuitBreaker, $service); + } + + public function getException(): Exception + { + return $this->exception; + } +} diff --git a/src/Places/Closed.php b/src/Places/Closed.php index 979aa67..653281c 100644 --- a/src/Places/Closed.php +++ b/src/Places/Closed.php @@ -4,6 +4,7 @@ use Resiliency\Contracts\Client; use Resiliency\Contracts\Transaction; +use Resiliency\Events\Failed; use Resiliency\Events\Tried; use Resiliency\Exceptions\UnavailableService; use Resiliency\States; @@ -55,6 +56,7 @@ public function call(Transaction $transaction, callable $fallback): string return $response; } catch (UnavailableService $exception) { + $this->dispatch(new Failed($this->circuitBreaker, $service, $exception)); $transaction->incrementFailures(); $storage->saveTransaction($service->getUri(), $transaction); diff --git a/src/Systems/MainSystem.php b/src/Systems/MainSystem.php index 8cb668f..f53b642 100644 --- a/src/Systems/MainSystem.php +++ b/src/Systems/MainSystem.php @@ -99,7 +99,7 @@ private static function validateTimeout(int $timeout): bool { // @doc https://www.php.net/manual/info.configuration.php the timeout is in seconds $maxExecutionTime = ini_get('max_execution_time'); - + $timeoutInSeconds = (int) ($timeout / 1000); return (0 === (int) $maxExecutionTime) || ($maxExecutionTime >= $timeoutInSeconds); diff --git a/tests/SymfonyCircuitBreakerEventsTest.php b/tests/SymfonyCircuitBreakerEventsTest.php index 1278741..9dc3d51 100644 --- a/tests/SymfonyCircuitBreakerEventsTest.php +++ b/tests/SymfonyCircuitBreakerEventsTest.php @@ -7,6 +7,7 @@ use ReflectionClass; use ReflectionException; use Resiliency\Contracts\CircuitBreaker; +use Resiliency\Events\Failed; use Resiliency\Events\Initiated; use Resiliency\Events\Isolated; use Resiliency\Events\Opened; @@ -54,12 +55,14 @@ public function testCircuitBreakerEventsOnFirstFailedCall(): void * then the conditions are met to open the circuit breaker */ $invocations = self::invocations($this->spy); - self::assertCount(4, $invocations); + self::assertCount(6, $invocations); self::assertInstanceOf(Initiated::class, $invocations[0]->getParameters()[0]); self::assertInstanceOf(Tried::class, $invocations[1]->getParameters()[0]); - self::assertInstanceOf(Tried::class, $invocations[2]->getParameters()[0]); - self::assertInstanceOf(Opened::class, $invocations[3]->getParameters()[0]); + self::assertInstanceOf(Failed::class, $invocations[2]->getParameters()[0]); + self::assertInstanceOf(Tried::class, $invocations[3]->getParameters()[0]); + self::assertInstanceOf(Failed::class, $invocations[4]->getParameters()[0]); + self::assertInstanceOf(Opened::class, $invocations[5]->getParameters()[0]); } public function testCircuitBreakerEventsOnIsolationAndResetActions(): void @@ -79,8 +82,8 @@ public function testCircuitBreakerEventsOnIsolationAndResetActions(): void * the related event has been dispatched */ $invocations = self::invocations($this->spy); - self::assertCount(5, $invocations); - self::assertInstanceOf(Isolated::class, $invocations[4]->getParameters()[0]); + self::assertCount(7, $invocations); + self::assertInstanceOf(Isolated::class, $invocations[6]->getParameters()[0]); /* * And now we reset the circuit breaker! @@ -88,8 +91,8 @@ public function testCircuitBreakerEventsOnIsolationAndResetActions(): void */ $circuitBreaker->reset($service); $invocations = self::invocations($this->spy); - self::assertCount(6, $invocations); - self::assertInstanceOf(Reseted::class, $invocations[5]->getParameters()[0]); + self::assertCount(8, $invocations); + self::assertInstanceOf(Reseted::class, $invocations[7]->getParameters()[0]); } private function createCircuitBreaker(): CircuitBreaker