Skip to content

Commit

Permalink
Merge pull request #2 from shouze/add-healthcheck-component
Browse files Browse the repository at this point in the history
Add Healthcheck component
  • Loading branch information
tyx committed Nov 30, 2017
2 parents b4e30d2 + 8fdd557 commit f0d2c83
Show file tree
Hide file tree
Showing 13 changed files with 684 additions and 0 deletions.
84 changes: 84 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
language: php

dist: trusty
sudo: false

git:
depth: 1

addons:
apt_packages:
- parallel

matrix:
include:
- php: 7.1.3
- php: 7.1
env: deps=high
- php: 7.2
fast_finish: true

before_install:
- |
# General configuration
stty cols 120
export COMPOSER_UP='composer update --no-progress --no-suggest --ansi'
export ATOUM='vendor/bin/atoum -ulr'
nanoseconds() {
local cmd="date"
local format="+%s%N"
local os=$(uname)
if hash gdate > /dev/null 2>&1; then
cmd="gdate"
elif [[ "$os" = Darwin ]]; then
format="+%s000000000"
fi
$cmd -u $format
}
export -f nanoseconds
# tfold is a helper to create folded reports
tfold () {
local title=$1
local fold=$(echo $title | sed -r 's/[^-_A-Za-z0-9]+/./g')
shift
local id=$(printf %08x $(( RANDOM * RANDOM )))
local start=$(nanoseconds)
echo -e "travis_fold:start:$fold"
echo -e "travis_time:start:$id"
echo -e "\\e[1;34m$title\\e[0m"
bash -xc "$*" 2>&1
local ok=$?
local end=$(nanoseconds)
echo -e "\\ntravis_time:end:$id:start=$start,finish=$end,duration=$(($end-$start))"
(exit $ok) &&
echo -e "\\e[32mOK\\e[0m $title\\n\\ntravis_fold:end:$fold" ||
echo -e "\\e[41mKO\\e[0m $title\\n"
(exit $ok)
}
export -f tfold
# php.ini configuration
INI=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
phpenv config-rm xdebug.ini || echo "xdebug not available"
echo date.timezone = Europe/Paris >> $INI
echo memory_limit = -1 >> $INI
echo session.gc_probability = 0 >> $INI
echo opcache.enable_cli = 1 >> $INI
- composer global require hirak/prestissimo
- COMPONENTS=$(find src -mindepth 3 -maxdepth 3 -type f -name .atoum.php -printf '%h\n')

install:
- |
run_tests () {
set -e
if [[ $deps = high ]]; then
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && $COMPOSER_UP && $ATOUM'"
elif [[ $deps = low ]]; then
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && $COMPOSER_UP --prefer-lowest --prefer-stable && $ATOUM'"
else
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && $COMPOSER_UP --prefer-stable && $ATOUM'"
fi
}
script:
- (run_tests)
14 changes: 14 additions & 0 deletions src/Component/Healthcheck/.atoum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/*
This file will automatically be included before EACH run.
Use it to configure atoum or anything that needs to be done before EACH run.
More information on documentation:
[en] http://docs.atoum.org/en/latest/chapter3.html#configuration-files
[fr] http://docs.atoum.org/fr/latest/lancement_des_tests.html#fichier-de-configuration
*/
use \mageekguy\atoum;

$report = $script->addDefaultReport();
// This will add a green or red logo after each run depending on its status.
$report->addField(new atoum\report\fields\runner\result\logo());
$runner->addTestsFromDirectory(__DIR__.'/tests/Units');
2 changes: 2 additions & 0 deletions src/Component/Healthcheck/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
27 changes: 27 additions & 0 deletions src/Component/Healthcheck/Healthcheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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;

interface Healthcheck
{
/**
* Informs if a destination is reachable
*
* @param string $destination A destination to join for the health check
*
* @throws InvalidDestination when the destination is not supported by health check implementation.
* @throws HealthcheckFailure when a non expected health check failure occurs.
*/
public function isReachable(string $destination): bool;
}
22 changes: 22 additions & 0 deletions src/Component/Healthcheck/HealthcheckFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;

final class HealthcheckFailure extends \RuntimeException
{
public static function cannotConnectToUri(string $uri)
{
return new static("Cannot connect to uri ${uri}.");
}
}
46 changes: 46 additions & 0 deletions src/Component/Healthcheck/HttpHealthcheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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(string $target): bool
{
$this->logger->info('Start HTTP healthcheck', ['target' => $target]);

$response = $this->httpClient->sendRequest(new Request('GET', $target));

$result = 200 === $response->getStatusCode();
$resultAsString = $result ? 'OK' : 'Fail';

$this->logger->info("[${resultAsString}] HTTP healthcheck", ['target' => $target]);

return $result;
}
}
22 changes: 22 additions & 0 deletions src/Component/Healthcheck/InvalidDestination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;

final class InvalidDestination extends \InvalidArgumentException
{
public static function ofProtocol(string $protocol)
{
return new static("Destination must be a valid ${protocol} uri.");
}
}
Loading

0 comments on commit f0d2c83

Please sign in to comment.