A health endpoint that checks its dependencies

An endpoint returning a constant ok tells a load balancer that PHP is running, which is not the question anybody is asking.

Route::get('/health', function () {
    $checks = [
        'mysql' => $this->check(function () { DB::select('SELECT 1'); }),
        'redis' => $this->check(function () { Redis::ping(); }),
        'queue' => $this->check(function () { Queue::size(); }),
    ];

    $ok = ! in_array(false, $checks, true);

    return response()->json(
        ['status' => $ok ? 'ok' : 'degraded', 'checks' => $checks],
        $ok ? 200 : 503
    );
});

The distinction worth making is between liveness — is this process wedged — and readiness — can it serve traffic right now. A dependency check belongs in readiness; putting it in liveness means a database blip restarts every application container simultaneously, which turns a small problem into an outage. Each check needs a short timeout of its own, or the health endpoint is the slowest thing on the server exactly when everything is slow.