<!-- Source: @site/projects/frontline-api-for-laravel/docs/12-status-and-health.md -->
<!-- Canonical: https://docs.pushery.com/frontline-api-for-laravel/status-and-health/ -->

# Status and health

> The status command that shows the sheets, the gateways and the meter, and the three health checks for the health page of your application.

Two views tell you whether the gateways work from current sheets and whether their counts arrive:
the status command, for a terminal or a deployment script, and the health checks, for the health
page of your application.

## The status command

`php artisan frontline:status` lists:

- every sheet last published, with its sequence, the time it went out, its age, when it expires,
  whether it was withdrawn because the key source no longer names its scope, for a key sheet the
  keys it lists, and when and why it last could not go out anew;
- every gateway that reported within the last three minutes, with its version, the protocol
  versions it reads, its mode, its product sheet and whether it holds the sheets published, and
  every gateway whose report cannot be read;
- the reading of the meter stream: when it was last read, how long the oldest batch not yet counted
  has waited, what was discarded, and per start of a gateway the batches read and lost;
- a block whose route the API host no longer has;
- the verdicts of the health checks below.

`--json` prints the same as one JSON object, and adds the reason of every refusal and of the last
discarded entry, which the text names only in the verdict of the check that fails or warns on it.
The command fails when a health check fails, as for a
stale sheet, a gateway that holds other sheets or a meter that stopped, so a deployment script can
stop on it. A warning alone leaves it successful. When the store cannot be read, the command names
the reason and fails, and `--json` prints it as `{"error": "…"}`.

## Health checks

`\Pushery\FrontlineApi\Health\FrontlineHealth` answers three checks for the health page of your
application. Each result names its check, a status of `ok`, `warning` or `failed`, and a line that
says what to do:

| Check | Fails when | Warns when |
|---|---|---|
| `sheets` | no product sheet was published or the one published has expired, a sheet could not go out anew, a sheet was last published longer ago than `frontline-api.health.sheet_stale_after_minutes` (22 hours by default, two past the renewal), except a withdrawn sheet, which is not renewed and lives out its lifetime, the store cannot be read, or a gateway reports other sheets two minutes after a publication | a gateway writes a report this package does not read, or a key sheet lists more than the 20,000 keys a gateway reloads in under a second |
| `meter` | the meter stream was not read within `frontline-api.health.meter_stale_after_minutes` (5 minutes by default), its oldest batch not yet counted is older than that, or the store cannot be read | a gateway lost batches, or an entry was discarded, within the last 24 hours |
| `signature` | in an environment other than `local` and `testing`: `frontline-api.gateway.require_signature` is off, `frontline-api.gateway.secret` holds no secret the gateway can sign with, or `frontline-api.gateway.addresses` holds no IP address | in an environment other than `local` and `testing`: `frontline-api.gateway.previous_secret` is set and holds no secret the gateway can sign with, or an entry of `frontline-api.gateway.addresses` is no IP address, such as a range, which matches no connection |

```php
use Pushery\FrontlineApi\Health\FrontlineHealth;

foreach (app(FrontlineHealth::class)->check() as $result) {
    $result->check;          // 'sheets', 'meter' or 'signature'
    $result->status->value;  // 'ok', 'warning' or 'failed'
    $result->message;
}
```

The package needs no health library. With one such as `spatie/laravel-health`, write one check of
your own per result: it calls `check()`, takes the result whose `check` it reports, and answers ok,
a warning or a failure with the result's `message`, as the library's own result type says it.

None of the checks listens to Laravel's `DiagnosingHealth` event, which `/up` and the gateway's
health probe fire. A stale sheet or a stopped meter shows on your health page and never takes a
server out of the gateway's rotation.
