💗 Health check for external services that are important for your application.
The best way to install 68publishers/health-check is using Composer:
$ composer require 68publishers/health-checkuse SixtyEightPublishers\HealthCheck\ExportMode;
use SixtyEightPublishers\HealthCheck\HealthChecker;
use SixtyEightPublishers\HealthCheck\ServiceChecker\PDOServiceChecker;
use SixtyEightPublishers\HealthCheck\ServiceChecker\RedisServiceChecker;
$checker = new HealthChecker();
$checker->addServiceChecker(new PDOServiceChecker('pgsql:host=127.0.0.1;port=5432;dbname=example', 'user', 'password'));
$checker->addServiceChecker(new RedisServiceChecker())
# check all services
$result = $checker->check();
# you can throw an exception
if (!$result->isOk()) {
throw $result->getError();
}
# or covert the result into a JSON
echo json_encode($result);
# check Redis only
$result = $checker->check(['redis']);
# check in the "Full" mode. The default mode is "Simple".
$result = $checker->check(NULL, ExportMode::Full);
# the result now contains detailed information about each service
echo json_encode($result);- PDO -
SixtyEightPublishers\HealthCheck\ServiceChecker\PDOServiceChecker - Doctrine DBAL -
SixtyEightPublishers\HealthCheck\ServiceChecker\DbalConnectionServiceChecker - Redis -
SixtyEightPublishers\HealthCheck\ServiceChecker\RedisServiceChecker - Http -
SixtyEightPublishers\HealthCheck\ServiceChecker\HttpServiceChecker
You can create your own service checker. Just create a class that implements the interface ServiceCheckerInterface.
The package provides compiler extensions for easy integration with Nette Framework.
extensions:
68publishers.health_check: SixtyEightPublishers\HealthCheck\Bridge\Nette\DI\HealthCheckExtension
68publishers.health_check:
service_checkers:
- SixtyEightPublishers\HealthCheck\ServiceChecker\RedisServiceChecker()
- SixtyEightPublishers\HealthCheck\ServiceChecker\PDOServiceChecker::fromParams([
driver: pgsql
host: '127.0.0.1'
port: 5432
dbname: example
user: user
password: password
])
- MyCustomServiceChecker('foo')
export_mode: full_if_debug # This is the default value. Supported values are "full_if_debug", "full", "simple" or custom service that implements an interface "ExportModeResolverInterface".Now the service of type SixtyEightPublishers\HealthCheck\HealthCheckerInterface is accessible in DIC.
extensions:
68publishers.health_check: SixtyEightPublishers\HealthCheck\Bridge\Nette\DI\HealthCheckExtension
68publishers.health_check.console: SixtyEightPublishers\HealthCheck\Bridge\Nette\DI\HealthCheckConsoleExtensionNow you can run this command:
$ bin/console health-check [<services>] [--export-mode <mode>]extensions:
68publishers.health_check: SixtyEightPublishers\HealthCheck\Bridge\Nette\DI\HealthCheckExtension
68publishers.health_check.application: SixtyEightPublishers\HealthCheck\Bridge\Nette\DI\HealthCheckApplicationExtension
68publishers.health_check.application:
route: '/health-check' # The default value. You can change it or set it as "false".The extension automatically appends the health check route into your RouteList. If you want to disable this behaviour, please set the option route to false and add the route to your route factory manually e.g.:
<?php
namespace App;
use Nette\Application\Routers\RouteList;
use SixtyEightPublishers\HealthCheck\Bridge\Nette\Application\HealthCheckRoute;
class RouteFactory {
public static function create(): RouteList {
$router = new RouteList();
$router->add(new HealthCheckRoute('/health-check'));
# ... other routes ...
return $router;
}
}Now you can check your services through an endpoint your-domain.com/health-check.
The endpoint returns the status code 200 if everything is ok and 503 if some service check failed.
Before opening a pull request, please check your changes using the following commands
$ make init # to pull and start all docker images
$ make cs.check
$ make stan
$ make tests.all