From 4f90ff4d961989df9d4e4359bd8d5dcca8356f5b Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Fri, 16 Jul 2021 19:58:06 +0200 Subject: [PATCH 1/3] Add healthz endpoint support --- src/lib/Client/DaprClient.php | 2 ++ src/lib/Client/DaprHttpClient.php | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/lib/Client/DaprClient.php b/src/lib/Client/DaprClient.php index a2b3f87..741bddb 100644 --- a/src/lib/Client/DaprClient.php +++ b/src/lib/Client/DaprClient.php @@ -420,6 +420,8 @@ abstract public function getBulkSecretAsync(string $storeName, array $metadata = */ abstract public function getBulkSecret(string $storeName, array $metadata = []): array; + abstract public function isDaprHealthy(): bool; + /** * @param string $token * @return null|array{dapr-api-token: string} diff --git a/src/lib/Client/DaprHttpClient.php b/src/lib/Client/DaprHttpClient.php index 5472c1c..8f4c790 100644 --- a/src/lib/Client/DaprHttpClient.php +++ b/src/lib/Client/DaprHttpClient.php @@ -43,4 +43,17 @@ public function __construct( ] ); } + + public function isDaprHealthy(): bool + { + try { + $result = $this->httpClient->get('/v1.0/healthz'); + if (200 === $result->getStatusCode()) { + return true; + } + return false; + } catch (\Throwable $exception) { + return false; + } + } } From 7576691fc9a9d32fa94016a4e386957ff7804ad5 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Fri, 16 Jul 2021 19:58:50 +0200 Subject: [PATCH 2/3] Add doc string --- src/lib/Client/DaprClient.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib/Client/DaprClient.php b/src/lib/Client/DaprClient.php index 741bddb..75fd6aa 100644 --- a/src/lib/Client/DaprClient.php +++ b/src/lib/Client/DaprClient.php @@ -420,6 +420,11 @@ abstract public function getBulkSecretAsync(string $storeName, array $metadata = */ abstract public function getBulkSecret(string $storeName, array $metadata = []): array; + /** + * Check if the daprd instance is up and running. + * + * @return bool True if it is running, else false. + */ abstract public function isDaprHealthy(): bool; /** From 48617a998402af84a36ac765411dcb1554298c71 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Fri, 16 Jul 2021 20:10:50 +0200 Subject: [PATCH 3/3] Add tests --- tests/HealthTest.php | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/HealthTest.php diff --git a/tests/HealthTest.php b/tests/HealthTest.php new file mode 100644 index 0000000..0eebff8 --- /dev/null +++ b/tests/HealthTest.php @@ -0,0 +1,48 @@ +get_http_client_stack( + [ + new Response(200) + ] + ); + $client = $this->get_new_client_with_http($container->client); + $this->assertTrue($client->isDaprHealthy()); + $request = $container->history[0]['request']; + $this->assertRequestUri('/v1.0/healthz', $request); + } + + public function testIsNotHealthy() { + $container = $this->get_http_client_stack( + [ + new Response(500) + ] + ); + $client = $this->get_new_client_with_http($container->client); + $this->assertFalse($client->isDaprHealthy()); + $request = $container->history[0]['request']; + $this->assertRequestUri('/v1.0/healthz', $request); + } + + public function testTimeout() { + $container = $this->get_http_client_stack( + [ + new RequestException('timed out', new Request('GET', 'test')) + ] + ); + $client = $this->get_new_client_with_http($container->client); + $this->assertFalse($client->isDaprHealthy()); + $request = $container->history[0]['request']; + $this->assertRequestUri('/v1.0/healthz', $request); + } +}