Skip to content

Commit f87e3d3

Browse files
Add isDaprHealthy function to client (#100)
* Add healthz endpoint support * Add doc string * Add tests
1 parent debcd71 commit f87e3d3

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/lib/Client/DaprClient.php

+7
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,13 @@ abstract public function getBulkSecretAsync(string $storeName, array $metadata =
420420
*/
421421
abstract public function getBulkSecret(string $storeName, array $metadata = []): array;
422422

423+
/**
424+
* Check if the daprd instance is up and running.
425+
*
426+
* @return bool True if it is running, else false.
427+
*/
428+
abstract public function isDaprHealthy(): bool;
429+
423430
/**
424431
* @param string $token
425432
* @return null|array{dapr-api-token: string}

src/lib/Client/DaprHttpClient.php

+13
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ public function __construct(
4343
]
4444
);
4545
}
46+
47+
public function isDaprHealthy(): bool
48+
{
49+
try {
50+
$result = $this->httpClient->get('/v1.0/healthz');
51+
if (200 === $result->getStatusCode()) {
52+
return true;
53+
}
54+
return false;
55+
} catch (\Throwable $exception) {
56+
return false;
57+
}
58+
}
4659
}

tests/HealthTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use GuzzleHttp\Exception\RequestException;
4+
use GuzzleHttp\Psr7\Request;
5+
use GuzzleHttp\Psr7\Response;
6+
7+
/**
8+
* Class HealthTest
9+
*/
10+
class HealthTest extends DaprTests
11+
{
12+
public function testIsHealthy()
13+
{
14+
$container = $this->get_http_client_stack(
15+
[
16+
new Response(200)
17+
]
18+
);
19+
$client = $this->get_new_client_with_http($container->client);
20+
$this->assertTrue($client->isDaprHealthy());
21+
$request = $container->history[0]['request'];
22+
$this->assertRequestUri('/v1.0/healthz', $request);
23+
}
24+
25+
public function testIsNotHealthy() {
26+
$container = $this->get_http_client_stack(
27+
[
28+
new Response(500)
29+
]
30+
);
31+
$client = $this->get_new_client_with_http($container->client);
32+
$this->assertFalse($client->isDaprHealthy());
33+
$request = $container->history[0]['request'];
34+
$this->assertRequestUri('/v1.0/healthz', $request);
35+
}
36+
37+
public function testTimeout() {
38+
$container = $this->get_http_client_stack(
39+
[
40+
new RequestException('timed out', new Request('GET', 'test'))
41+
]
42+
);
43+
$client = $this->get_new_client_with_http($container->client);
44+
$this->assertFalse($client->isDaprHealthy());
45+
$request = $container->history[0]['request'];
46+
$this->assertRequestUri('/v1.0/healthz', $request);
47+
}
48+
}

0 commit comments

Comments
 (0)