-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpHealthcheck.php
52 lines (40 loc) · 1.34 KB
/
HttpHealthcheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/*
* This file is part of the Ubirak package.
*
* (c) Ubirak team <team@ubirak.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Ubirak\Component\Healthcheck;
use GuzzleHttp\Psr7\Request;
use Http\Client\HttpClient;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
final class HttpHealthcheck implements Healthcheck
{
private $httpClient;
private $logger;
public function __construct(HttpClient $httpClient, LoggerInterface $logger = null)
{
$this->httpClient = $httpClient;
$this->logger = $logger ?? new NullLogger();
}
public function isReachable(Destination $destination): bool
{
$url = (string) $destination;
$this->logger->info('Start HTTP healthcheck', ['destination' => $url]);
try {
$response = $this->httpClient->sendRequest(new Request('GET', $url));
} catch (\Exception $e) {
$this->logger->info('[Fail] HTTP healthcheck', ['destination' => $url]);
return false;
}
$result = 200 === $response->getStatusCode();
$resultAsString = $result ? 'OK' : 'Fail';
$this->logger->info("[${resultAsString}] HTTP healthcheck", ['destination' => $url]);
return $result;
}
}