Functional Weave
Code in TypeScript

monitor.heartbeat

Whether a cron job that pings on a schedule is new, up, late or down, from its last ping, period and grace time.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 14 tests, run in TypeScript, Python and Rust.

What it does

The state of a heartbeat check (a "dead man's switch"): a cron job, backup or worker pings the monitor each time it runs, and the monitor raises the alarm when the pings stop. This is the shape healthchecks.io, Cronitor and Uptime Kuma's push monitors use, with healthchecks.io's vocabulary:

| state | healthchecks.io says | here | | --- | --- | --- | | `new` | "A newly created check that has not received any pings yet." | `lastPingAt` is null | | `up` | "The last success signal has arrived on time." | `now <= dueAt` | | `late` | "The success signal is due but has not arrived yet. It is not yet late by more than the check's configured Grace Time." | `dueAt < now <= downAt` | | `down` | "The success signal has not arrived yet, and the Grace Time has elapsed." | `now > downAt` |

For example

  • heartbeatStatus(—, 5,000, 3,600, 600) → state new, due at —, down at —, overdue seconds 0 never pinged is new
  • heartbeatStatus(1,000, 1,000, 3,600, 600) → state up, due at 4,600, down at 5,200, overdue seconds 0 just pinged is up
  • heartbeatStatus(1,000, 4,600, 3,600, 600) → state up, due at 4,600, down at 5,200, overdue seconds 0 the due second itself is still up

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

export function heartbeatStatus(lastPingAt: number | null, now: number, intervalSeconds: number, graceSeconds: number): HeartbeatStatus
lastPingAtint?Unix seconds of the last ping received; null when it has never pinged
nowintUnix seconds
intervalSecondsintthe expected time between pings (the period), at least 1
graceSecondsinthow long past due before it counts as down, 0 or more
returnsHeartbeatStatus

The types it declares, generated into your project

export type HeartbeatState = "new" | "up" | "late" | "down";

/** Where a heartbeat stands and when it changes next. */
export interface HeartbeatStatus {
  readonly state: HeartbeatState;
  /** when the next ping is due: lastPingAt + intervalSeconds; null when new */
  readonly dueAt: number | null;
  /** the last second it counts as late: dueAt + graceSeconds; null when new */
  readonly downAt: number | null;
  /** now - dueAt when late or down, else 0 */
  readonly overdueSeconds: number;
}

Your code names it in one line, in the file that uses it

import { heartbeatStatus } from "#fune/monitor.heartbeat@^1";
impl/typescript.ts · 25 lines · open · raw
import { type HeartbeatStatus } from "./monitor_heartbeat_types.ts";

function whole(name: string, v: number): void {
  if (!Number.isSafeInteger(v)) throw new RangeError(`${name} must be whole seconds, received ${v}`);
}

/**
 * New, up, late or down, as healthchecks.io names them. Up through the due
 * second itself, late through the last second of the grace time, down after.
 * A ping stamped in the future (clock skew between the job and the monitor)
 * is simply up: its due time is later still.
 */
export function heartbeatStatus(lastPingAt: number | null, now: number, intervalSeconds: number, graceSeconds: number): HeartbeatStatus {
  if (lastPingAt !== null) whole("lastPingAt", lastPingAt);
  whole("now", now);
  whole("intervalSeconds", intervalSeconds);
  whole("graceSeconds", graceSeconds);
  if (intervalSeconds < 1) throw new RangeError(`intervalSeconds must be at least 1, received ${intervalSeconds}`);
  if (graceSeconds < 0) throw new RangeError(`graceSeconds must not be negative, received ${graceSeconds}`);
  if (lastPingAt === null) return { state: "new", dueAt: null, downAt: null, overdueSeconds: 0 };
  const dueAt = lastPingAt + intervalSeconds;
  const downAt = dueAt + graceSeconds;
  if (now <= dueAt) return { state: "up", dueAt, downAt, overdueSeconds: 0 };
  return { state: now <= downAt ? "late" : "down", dueAt, downAt, overdueSeconds: now - dueAt };
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add monitor.heartbeat
Download for TypeScript monitor.heartbeat-1.0.0-typescript.fune · 8,726 bytes sha256 c8b5ab5359a8c5a0502f70256e2fa8666afee12ccc53987d08bd372fc1588a61

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./monitor.heartbeat-1.0.0-typescript.fune, or fetch it from a terminal with fune pull monitor.heartbeat@1.0.0:typescript.

The whole function, every language, is one file too: monitor.heartbeat-1.0.0.fune, 12,774 bytes, sha256 670066e850a8ae45f319f6644290afddae8eaec2c718edba53e6b8a7a51a80f3. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before monitor.heartbeat

after — your function gets the result and the arguments, and returns the final result.

// fune: after monitor.heartbeat

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show monitor.heartbeat --steps.

// fune: step monitor.heartbeat after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
never pinged is new —, 5,000, 3,600, 600 → state new, due at —, down at —, overdue seconds 0
just pinged is up 1,000, 1,000, 3,600, 600 → state up, due at 4,600, down at 5,200, overdue seconds 0
the due second itself is still up 1,000, 4,600, 3,600, 600 → state up, due at 4,600, down at 5,200, overdue seconds 0
one second past due is late 1,000, 4,601, 3,600, 600 → state late, due at 4,600, down at 5,200, overdue seconds 1
the last second of the grace time is still late 1,000, 5,200, 3,600, 600 → state late, due at 4,600, down at 5,200, overdue seconds 600
one second after the grace time is down 1,000, 5,201, 3,600, 600 → state down, due at 4,600, down at 5,200, overdue seconds 601
a job silent for a day is down and says by how much 1,000, 90,000, 3,600, 600 → state down, due at 4,600, down at 5,200, overdue seconds 85,400
no grace time goes straight from up to down 1,000, 4,601, 3,600, 0 → state down, due at 4,600, down at 4,600, overdue seconds 1
a ping stamped in the future (clock skew) is up, not overdue 2,000, 1,000, 3,600, 600 → state up, due at 5,600, down at 6,200, overdue seconds 0
a one-minute cron job with a five-minute grace 1,767,225,600, 1,767,225,720, 60, 300 → state late, due at 1,767,225,660, down at 1,767,225,960, overdue seconds 60
Show the other 4 tests
CaseArgumentsExpected
a zero interval is an error 1,000, 2,000, 0, 600 → error: intervalSeconds must be at least 1, received 0
a negative grace time is an error 1,000, 2,000, 3,600, -1 → error: graceSeconds must not be negative, received -1
a zero interval is an error even before the first ping —, 2,000, 0, 600 → error: intervalSeconds must be at least 1, received 0
a fractional now is an error 1,000, 1.5, 3,600, 600 → error: now must be whole seconds, received 1.5

More from the author

where `dueAt = lastPingAt + intervalSeconds` and `downAt = dueAt + graceSeconds`. The due second itself is still up, and the last second of the grace time is still late; the state changes the second after. With no grace time a check goes straight from up to down.

`overdueSeconds` is `now - dueAt` when late or down (how long past due, for "late by 2m" in an alert), and 0 otherwise. `dueAt` and `downAt` are returned so the caller can schedule its next evaluation instead of polling.

## Edge cases

- A ping stamped after `now` (the job's clock is ahead of the monitor's) is `up`, overdue 0: its due time is later still. It is not an error, because clock skew is normal and refusing it would hide a healthy job. - `paused` is not a state here: pausing is the caller's decision, not something the ping times say. - Only simple period schedules are covered; a cron-expression schedule gives `dueAt` from the expression instead.

## Errors

- `intervalSeconds must be at least 1, received X` - `graceSeconds must not be negative, received X` - `NAME must be whole seconds, received X` for a fractional time

## Sources

- healthchecks.io documentation, "Check states" and "Period and Grace Time": https://healthchecks.io/docs/ and https://healthchecks.io/docs/configuring_checks/

Files

PathBytes
README.md2,099
impl/python.py1,550
impl/rust.rs2,325
impl/typescript.ts1,396
vectors.json2,291