antonioribeiro / health

Laravel Health Panel
BSD 3-Clause "New" or "Revised" License
1.95k stars 197 forks source link

Get health information in code #131

Open bastian-schur opened 5 years ago

bastian-schur commented 5 years ago

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.

poor-bob commented 5 years ago

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);
        }
    }
}
antonioribeiro commented 5 years ago

You can probably do

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

// or 

$databaseHealthy = app('pragmarx.health')->checkResource('database')->isHealthy();
antonioribeiro commented 5 years ago

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');
poor-bob commented 5 years ago

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!';
    }
}