antonioribeiro / health

Laravel Health Panel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get health information in code

bastian-schur opened this issue · 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.

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

You can probably do

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

// or 

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

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

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