Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get health information in code #131

Open
bastian-schur opened this issue Oct 25, 2018 · 4 comments
Open

Get health information in code #131

bastian-schur opened this issue Oct 25, 2018 · 4 comments

Comments

@bastian-schur
Copy link

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
Copy link

poor-bob commented Nov 6, 2018

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
Copy link
Owner

antonioribeiro commented Nov 6, 2018

You can probably do

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

// or 

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

@antonioribeiro
Copy link
Owner

antonioribeiro commented Nov 6, 2018

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
Copy link

poor-bob commented Nov 6, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@bastian-schur @antonioribeiro @poor-bob and others