Skip to content

Commit f0d2c83

Browse files
authored
Merge pull request #2 from shouze/add-healthcheck-component
Add Healthcheck component
2 parents b4e30d2 + 8fdd557 commit f0d2c83

File tree

13 files changed

+684
-0
lines changed

13 files changed

+684
-0
lines changed

.travis.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
language: php
2+
3+
dist: trusty
4+
sudo: false
5+
6+
git:
7+
depth: 1
8+
9+
addons:
10+
apt_packages:
11+
- parallel
12+
13+
matrix:
14+
include:
15+
- php: 7.1.3
16+
- php: 7.1
17+
env: deps=high
18+
- php: 7.2
19+
fast_finish: true
20+
21+
before_install:
22+
- |
23+
# General configuration
24+
stty cols 120
25+
export COMPOSER_UP='composer update --no-progress --no-suggest --ansi'
26+
export ATOUM='vendor/bin/atoum -ulr'
27+
nanoseconds() {
28+
local cmd="date"
29+
local format="+%s%N"
30+
local os=$(uname)
31+
if hash gdate > /dev/null 2>&1; then
32+
cmd="gdate"
33+
elif [[ "$os" = Darwin ]]; then
34+
format="+%s000000000"
35+
fi
36+
$cmd -u $format
37+
}
38+
export -f nanoseconds
39+
# tfold is a helper to create folded reports
40+
tfold () {
41+
local title=$1
42+
local fold=$(echo $title | sed -r 's/[^-_A-Za-z0-9]+/./g')
43+
shift
44+
local id=$(printf %08x $(( RANDOM * RANDOM )))
45+
local start=$(nanoseconds)
46+
echo -e "travis_fold:start:$fold"
47+
echo -e "travis_time:start:$id"
48+
echo -e "\\e[1;34m$title\\e[0m"
49+
bash -xc "$*" 2>&1
50+
local ok=$?
51+
local end=$(nanoseconds)
52+
echo -e "\\ntravis_time:end:$id:start=$start,finish=$end,duration=$(($end-$start))"
53+
(exit $ok) &&
54+
echo -e "\\e[32mOK\\e[0m $title\\n\\ntravis_fold:end:$fold" ||
55+
echo -e "\\e[41mKO\\e[0m $title\\n"
56+
(exit $ok)
57+
}
58+
export -f tfold
59+
60+
# php.ini configuration
61+
INI=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
62+
phpenv config-rm xdebug.ini || echo "xdebug not available"
63+
echo date.timezone = Europe/Paris >> $INI
64+
echo memory_limit = -1 >> $INI
65+
echo session.gc_probability = 0 >> $INI
66+
echo opcache.enable_cli = 1 >> $INI
67+
- composer global require hirak/prestissimo
68+
- COMPONENTS=$(find src -mindepth 3 -maxdepth 3 -type f -name .atoum.php -printf '%h\n')
69+
70+
install:
71+
- |
72+
run_tests () {
73+
set -e
74+
if [[ $deps = high ]]; then
75+
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && $COMPOSER_UP && $ATOUM'"
76+
elif [[ $deps = low ]]; then
77+
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && $COMPOSER_UP --prefer-lowest --prefer-stable && $ATOUM'"
78+
else
79+
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && $COMPOSER_UP --prefer-stable && $ATOUM'"
80+
fi
81+
}
82+
83+
script:
84+
- (run_tests)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/*
3+
This file will automatically be included before EACH run.
4+
Use it to configure atoum or anything that needs to be done before EACH run.
5+
More information on documentation:
6+
[en] http://docs.atoum.org/en/latest/chapter3.html#configuration-files
7+
[fr] http://docs.atoum.org/fr/latest/lancement_des_tests.html#fichier-de-configuration
8+
*/
9+
use \mageekguy\atoum;
10+
11+
$report = $script->addDefaultReport();
12+
// This will add a green or red logo after each run depending on its status.
13+
$report->addField(new atoum\report\fields\runner\result\logo());
14+
$runner->addTestsFromDirectory(__DIR__.'/tests/Units');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Ubirak package.
5+
*
6+
* (c) Ubirak team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ubirak\Component\Healthcheck;
15+
16+
interface Healthcheck
17+
{
18+
/**
19+
* Informs if a destination is reachable
20+
*
21+
* @param string $destination A destination to join for the health check
22+
*
23+
* @throws InvalidDestination when the destination is not supported by health check implementation.
24+
* @throws HealthcheckFailure when a non expected health check failure occurs.
25+
*/
26+
public function isReachable(string $destination): bool;
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Ubirak package.
5+
*
6+
* (c) Ubirak team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ubirak\Component\Healthcheck;
15+
16+
final class HealthcheckFailure extends \RuntimeException
17+
{
18+
public static function cannotConnectToUri(string $uri)
19+
{
20+
return new static("Cannot connect to uri ${uri}.");
21+
}
22+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Ubirak package.
5+
*
6+
* (c) Ubirak team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ubirak\Component\Healthcheck;
15+
16+
use GuzzleHttp\Psr7\Request;
17+
use Http\Client\HttpClient;
18+
use Psr\Log\LoggerInterface;
19+
use Psr\Log\NullLogger;
20+
21+
final class HttpHealthcheck implements Healthcheck
22+
{
23+
private $httpClient;
24+
25+
private $logger;
26+
27+
public function __construct(HttpClient $httpClient, LoggerInterface $logger = null)
28+
{
29+
$this->httpClient = $httpClient;
30+
$this->logger = $logger ?? new NullLogger();
31+
}
32+
33+
public function isReachable(string $target): bool
34+
{
35+
$this->logger->info('Start HTTP healthcheck', ['target' => $target]);
36+
37+
$response = $this->httpClient->sendRequest(new Request('GET', $target));
38+
39+
$result = 200 === $response->getStatusCode();
40+
$resultAsString = $result ? 'OK' : 'Fail';
41+
42+
$this->logger->info("[${resultAsString}] HTTP healthcheck", ['target' => $target]);
43+
44+
return $result;
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Ubirak package.
5+
*
6+
* (c) Ubirak team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ubirak\Component\Healthcheck;
15+
16+
final class InvalidDestination extends \InvalidArgumentException
17+
{
18+
public static function ofProtocol(string $protocol)
19+
{
20+
return new static("Destination must be a valid ${protocol} uri.");
21+
}
22+
}

0 commit comments

Comments
 (0)