health icon indicating copy to clipboard operation
health copied to clipboard

Get health information in code

Open bastian-schur opened this issue 6 years ago • 4 comments

Hi,

is there a way to get the health information in code directly? If i want to show some checks in my application itself without using the panel it would be nice to have a function like "Health::getChecks()" to get informations about the health status.

bastian-schur avatar Oct 25 '18 11:10 bastian-schur

I do something similar like this

<?php

namespace App\Http\Controllers;

use PragmaRX\Health\Http\Controllers\Health;

class HealthCheckController extends Health {

    public function index() {
        $json = $this->check();
        $healthy = $json->original['Health']['health']['healthy'];

        if ($healthy){
            return 'Healthy!';
        } else {
            abort(500);
        }
    }
}

poor-bob avatar Nov 06 '18 03:11 poor-bob

You can probably do

$generalHealthState = app('pragmarx.health')->checkResources();

// or 

$databaseHealthy = app('pragmarx.health')->checkResource('database')->isHealthy();

antonioribeiro avatar Nov 06 '18 04:11 antonioribeiro

Also:

Artisan::command('database:health', function () {
    app('pragmarx.health')->checkResource('database')->isHealthy()
        ? $this->info('database is healthy')
        : $this->info('database is in trouble')
    ;
})->describe('Check database health');

antonioribeiro avatar Nov 06 '18 04:11 antonioribeiro

Here's checking all resources to come to a final boolean since Health.yml has been deprecated

<?php

namespace App\Http\Controllers;

use PragmaRX\Health\Http\Controllers\Health;

class HealthCheckController extends Controller {

    public function index() {
        $services = array_map('strtolower', config('health.resources.enabled'));

        foreach($services as $service){
            $serviceHealth = app('pragmarx.health')->checkResource($service)->isHealthy();
            if (!$serviceHealth) {
                abort(500);
            }
        }
        return 'Healthy!';
    }
}

poor-bob avatar Nov 06 '18 17:11 poor-bob