PHP code example of tiny-blocks / http-health-check

1. Go to this page and download the library: Download tiny-blocks/http-health-check library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tiny-blocks / http-health-check example snippets




declare(strict_types=1);

use TinyBlocks\HttpHealthCheck\LivenessHandler;

# LivenessHandler is a PSR-15 RequestHandlerInterface.
$handler = LivenessHandler::create();

# Route your liveness endpoint (for example, GET /health/live) to this handler.
# handle() returns a PSR-7 response with status 200 and body {"status":"UP"}.
$response = $handler->handle($request);



declare(strict_types=1);

use Aws\Ssm\SsmClient;
use Doctrine\DBAL\DriverManager;
use TinyBlocks\HttpHealthCheck\DoctrineHealthCheck;
use TinyBlocks\HttpHealthCheck\HealthChecks;
use TinyBlocks\HttpHealthCheck\ReadinessHandler;
use TinyBlocks\HttpHealthCheck\SsmHealthCheck;

$connection = DriverManager::getConnection(['url' => 'pdo-pgsql://user:pass@localhost:5432/app']);
$client = new SsmClient(['region' => 'us-east-1', 'version' => 'latest']);

# A failing critical check makes the service UNAVAILABLE (503).
# A failing optional check only marks the service DEGRADED (still 200).
$checks = HealthChecks::createFromEmpty()
    ->withCritical(check: DoctrineHealthCheck::from(connection: $connection)->withName(name: 'primary'))
    ->withOptional(check: SsmHealthCheck::from(client: $client));

# Route your readiness endpoint (for example, GET /health/ready) to this handler.
$handler = ReadinessHandler::from(checks: $checks);

$response = $handler->handle($request);



declare(strict_types=1);

use Doctrine\DBAL\DriverManager;
use TinyBlocks\HttpHealthCheck\DoctrineHealthCheck;
use TinyBlocks\HttpHealthCheck\HealthChecks;

# doctrine/dbal is only althCheck::from(connection: $connection)
            ->withName(name: 'primary')
            ->withQuery(query: 'SELECT version()')
    );



declare(strict_types=1);

use Aws\Ssm\SsmClient;
use TinyBlocks\HttpHealthCheck\HealthChecks;
use TinyBlocks\HttpHealthCheck\SsmHealthCheck;

# aws/aws-sdk-php is only client)->withName(name: 'parameters'));



declare(strict_types=1);

use TinyBlocks\HttpHealthCheck\DrainMarker;
use TinyBlocks\HttpHealthCheck\HealthChecks;
use TinyBlocks\HttpHealthCheck\ReadinessHandler;

# Watch the default path (/tmp/draining).
$default = DrainMarker::default();

# Or watch a custom path.
$custom = DrainMarker::from(path: '/var/run/app/draining');

$handler = ReadinessHandler::from(checks: HealthChecks::createFromEmpty(), drainMarker: $custom);



declare(strict_types=1);

use Throwable;
use TinyBlocks\HttpHealthCheck\Health;
use TinyBlocks\HttpHealthCheck\HealthCheck;
use TinyBlocks\HttpHealthCheck\HealthChecks;

final readonly class RedisHealthCheck implements HealthCheck
{
    public function __construct(private Redis $client)
    {
    }

    public function name(): ?string
    {
        return null;
    }

    public function check(): Health
    {
        # Catch the failure and report DOWN rather than letting it escape.
        try {
            $this->client->ping();

            return Health::up();
        } catch (Throwable $exception) {
            return Health::down(detail: $exception->getMessage());
        }
    }

    public function component(): string
    {
        return 'cache';
    }
}

$checks = HealthChecks::createFromEmpty()->withOptional(check: new RedisHealthCheck(client: $redis));