# Heartbeat

> The heartbeat check - a cron job, timer or scheduled pipeline pings a URL, and a missing ping opens an incident. Period, grace, cron and OnCalendar schedules, and the ping URL.

A heartbeat check watches something that runs on a schedule: a backup, a cron job, a systemd timer, a nightly CI pipeline. The job calls a URL when it finishes. If no call arrives in time, the check is DOWN.

Heartbeat checks run on no node and have no quorum and no locations: `nodes` must be left out, and `intervalSecs` is not used.

```json
{ "type": "heartbeat", "periodSecs": 86400, "graceSecs": 1800 }
```

## Fields

| Field        | Type    | Default                     | Rules                                                                              |
| ------------ | ------- | --------------------------- | ---------------------------------------------------------------------------------- |
| `periodSecs` | integer | required without `schedule` | At least 30. A ping is due every period                                            |
| `graceSecs`  | integer | required                    | 0 to 10 periods. How late a ping may be                                            |
| `schedule`   | object  | none                        | Expect pings after each time of a calendar schedule instead of every period, below |

Without a schedule, the check is DOWN when no ping arrives within `periodSecs + graceSecs` of the last one (or of the check's creation).

## Schedules

When a job runs at fixed times, give its schedule: the deadline after a ping is then the schedule's next time plus `graceSecs`.

```json
{
  "type": "heartbeat",
  "graceSecs": 900,
  "schedule": { "syntax": "cron", "expression": "30 2 * * 1-5", "timezone": "Europe/Berlin" }
}
```

| Field        | Rules                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `syntax`     | `cron` or `on_calendar`                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `expression` | `cron`: 5 fields `minute hour day month day-of-week`, with names (`jan`, `mon`), `*`, lists, ranges, steps and the macros `@hourly`, `@daily`, `@weekly`, `@monthly`, `@yearly`. When both day of month and day of week are set, either one matches, as in Vixie cron. `on_calendar`: a systemd `OnCalendar` expression, `[weekdays] [date] [time]`, such as `Mon..Fri 02:00`, `*-*-01 06:30`, `hourly` or `daily`; a time zone inside the expression, `~` and fractional seconds are not accepted |
| `timezone`   | An IANA name such as `Europe/Berlin`. Without it, the workspace's time zone                                                                                                                                                                                                                                                                                                                                                                                                                        |

A time that does not exist on the day clocks move forward moves to the next existing time; an ambiguous one on the day clocks move back takes the first occurrence. With a schedule, `periodSecs` may be left out; responses give it as the shortest gap between the schedule's next times, which must be at least 30 seconds. A schedule that does not parse or never fires again is refused with `check.invalid_schedule`.

## The ping URL

Creating a heartbeat check returns `pingUrl` once, in the response of the create request (and in `monitorly apply` output). It looks like:

```text
https://gw.monitorly.cloud/hb/<token>
```

The token is the only credential: keep the URL like a password. If it leaks, replace it with `POST /api/v1/checks/{checkId}/heartbeat-token`; the old URL stops working and the new one is shown once.

| Request                          | Effect                                                |
| -------------------------------- | ----------------------------------------------------- |
| `GET` or `POST /hb/<token>`      | Success: the check is UP and the next deadline starts |
| `GET` or `POST /hb/<token>/fail` | The job reports a failure: the check is DOWN at once  |

Answers: `202` when the ping is recorded, `404` for an unknown token, `429` with `Retry-After` over the rate limit, `503` when it could not be recorded (retry).

Call the URL at the end of the job, so that a job that never finishes counts as missing:

```sh title="crontab"
30 2 * * 1-5  /usr/local/bin/backup && curl -fsS -m 10 --retry 3 https://gw.monitorly.cloud/hb/<token> > /dev/null
```

To report a failure as soon as it happens instead of waiting for the deadline:

```sh title="backup.sh"
if /usr/local/bin/backup; then
  curl -fsS -m 10 --retry 3 https://gw.monitorly.cloud/hb/<token>
else
  curl -fsS -m 10 --retry 3 https://gw.monitorly.cloud/hb/<token>/fail
fi
```

## Limits

- At most 1 ping per second per token, with bursts of 20.
- At most 20 pings per second from one client address, with bursts of 200.
- Over either limit the answer is `429` with `Retry-After`, and the ping is not recorded.

## Recovery

A successful ping after DOWN makes the check UP at once and closes the incident. Pause, maintenance and mute work as for other checks.
