68publishers/health-check

Check the status of an application services.

v0.1.2 2021-08-08 21:35 UTC

This package is auto-updated.

Last update: 2021-11-09 00:42:40 UTC


README

Build Status Quality Score Coverage Status Total Downloads Latest Version on Packagist

💗 Health check for external services that are important for your application.

Installation

The best way to install 68publishers/health-check is using Composer:

$ composer require 68publishers/health-check

Standalone usage

<?php

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();

if ($result->isOk()) {
    echo 'OK';
} else {
    throw $result->getError();
}

# check Redis only
$result = $checker->check(['redis']);

#...

Available service checkers

  • Database using PDO (SixtyEightPublishers\HealthCheck\ServiceChecker\PDOServiceChecker)
  • 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.

Integration into Nette Framework

The package provides compiler extensions for easy integration with Nette Framework.

Configuration example

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')

Now the service of type SixtyEightPublishers\HealthCheck\HealthCheckerInterface is accessible in DIC.

Health check using Symfony Console

extensions:
	68publishers.health_check.console: SixtyEightPublishers\HealthCheck\Bridge\Nette\DI\HealthCheckConsoleExtension

Now you can run this command:

$ bin/console health-check [<services>] [--full]

An array argument services represents the names of services for a check. The argument is optional so all services are checked when the argument is omitted.

An output is simplified by default:

$ bin/console health-check
{
    "status": "ok",
    "is_ok": true
}
$ bin/console health-check
{
    "status": "failed",
    "is_ok": false
}

But when an option --full is present the output contains information about all services:

$ bin/console health-check
{
    "status": "failed",
    "is_ok": false,
    "services": [
        {
            "name": "redis",
            "is_ok": true,
            "status": "running",
            "error": null
        },
        {
            "name": "database",
            "is_ok": true,
            "status": "running",
            "error": null
        },
        {
            "name": "foo",
            "is_ok": false,
            "status": "down",
            "error": "The service responds with a status code 500."
        }
    ]
}

Health check using an endpoint

Create a Presenter like this:

<?php

use SixtyEightPublishers\HealthCheck\HealthCheckerInterface;
use SixtyEightPublishers\HealthCheck\Bridge\Nette\UI\AbstractHealthCheckPresenter;

final class HealthCheckPresenter extends AbstractHealthCheckPresenter
{
    protected function getArrayExportMode() : string
    {
        # you can resolve an export mode in this method, use following constants:

        # 1) HealthCheckerInterface::ARRAY_EXPORT_MODEL_SIMPLE
        # 2) HealthCheckerInterface::ARRAY_EXPORT_MODE_FULL

        return HealthCheckerInterface::ARRAY_EXPORT_MODE_FULL;
    }
}

Then add a route in your Router Factory:

<?php

/** @var \Nette\Routing\Router $router */

$router->addRoute('health-check', 'HealthCheck:default');

Now you can check your services through an endpoint your-domain.com/health-check. The endpoint returns a status code 200 if everything is ok and 503 if some service check failed.

Contributing

Before committing any changes, don't forget to run

$ composer run php-cs-fixer

and

$ composer run tests