PHP code example of thesis / grpc-health

1. Go to this page and download the library: Download thesis/grpc-health 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/ */

    

thesis / grpc-health example snippets


use Grpc\Health\V1\HealthServerRegistry;
use Thesis\Grpc\Health;

$watchdog = new Health\Watchdog();

// The adapter depends on the Monitor, the generated registry wires it into the gRPC server.
$grpcServer->register(new HealthServerRegistry(new Health\Server($watchdog)));

use Thesis\Grpc\Health\Reporter;
use Thesis\Grpc\Health\ServingStatus;

final readonly class Orders
{
    public function __construct(
        private Reporter $health,
    ) {}

    public function boot(): void
    {
        $this->health->report('orders.v1.Orders', ServingStatus::Serving);
    }

    public function overloaded(): void
    {
        $this->health->report('orders.v1.Orders', ServingStatus::NotServing);
    }
}

$watchdog->shutdown(); // every service -> NotServing, further changes are frozen
$watchdog->resume();   // every service -> Serving again

use Grpc\Health\V1\HealthClient;
use Thesis\Grpc\Health;
use Thesis\Grpc\Health\ServingStatus;

$health = new Health\Client(new HealthClient($grpcClient));

$status = $health->statusOf('orders.v1.Orders'); // ServingStatus; unknown service -> ServiceUnknown

if ($health->serving('orders.v1.Orders')) {
    // ready — handy for readiness probes and health-check scripts
}

$watch = $health->watch('orders.v1.Orders', function (ServingStatus $status): void {
    // react to the new status
});

// later
$watch->close();