Heartbeats
A Heartbeat watches something that
should happen on a schedule: a cron job, a backup, a queue worker. The job
requests the heartbeat’s secret url every period seconds. When a request
doesn’t arrive within grace seconds after that, Better Stack opens an
incident. A monitor checks your service from outside; a heartbeat waits for
your service to check in.
- period
- 60 s
- grace
- 60 s
- Workerpings url →WorkerAlive
Create a heartbeat
Section titled “Create a heartbeat”A nightly backup that must report once a day, with two hours of slack:
const backup = yield* BetterStack.Incidents.Heartbeat("backup", { name: "Nightly database backup", period: 86400, grace: 7200, email: true, push: true,});The heartbeat stays pending until its first ping.
Give the job its URL
Section titled “Give the job its URL”The ping url is the whole credential: anyone who has it can ping the
heartbeat. It comes back Redacted. Export it from the stack, or pass it into
the job’s environment:
return { backupUrl: backup.url };Ping from a background Service
Section titled “Ping from a background Service”A background Cloud.Service gets the URL through env
and requests it every minute. When the worker stops, the heartbeat goes silent
and Better Stack opens an incident:
import { ServerHost } from "alchemy/Runtime/Process";import * as BetterStack from "betterstack-alchemy";import * as Config from "effect/Config";import * as Effect from "effect/Effect";import * as Redacted from "effect/Redacted";import * as Schedule from "effect/Schedule";import { Box, WorkerAlive } from "./shared.ts";
export default class Worker extends BetterStack.Cloud.Service<Worker>()( "Worker", Effect.gen(function* () { const heartbeat = yield* WorkerAlive; return { server: Box, main: import.meta.url, env: { HEARTBEAT_URL: heartbeat.url } }; }), Effect.gen(function* () { const host = yield* ServerHost; yield* host.run( Effect.gen(function* () { const url = yield* Config.Redacted("HEARTBEAT_URL"); yield* Effect.tryPromise(() => fetch(Redacted.value(url))).pipe( Effect.ignore, Effect.repeat(Schedule.spaced("1 minute")), ); }).pipe(Effect.orDie), ); }),) {}Ping through a binding
Section titled “Ping through a binding”Incidents.SendHeartbeat gives running code a typed ping. The host receives
only the heartbeat’s URL, as BETTERSTACK_HEARTBEAT_<HEARTBEAT>_URL, and no API
token. Provide SendHeartbeatHttp on the Service’s implementation:
const alive = yield* BetterStack.Incidents.SendHeartbeat(ExportDone);const watched = yield* BetterStack.Incidents.heartbeatJob(ExportDone);Then call it where the work finishes:
if (path === "/ping") { yield* alive(); return yield* HttpServerResponse.json({ done: "ping" });}Incidents.FailHeartbeat reports an explicit failure, so the incident opens at
once instead of after the missed period. Incidents.ReportHeartbeat sends an
exit code and output: 0 counts as a ping, anything else opens an incident with
the output attached.
Wrap a whole job
Section titled “Wrap a whole job”Incidents.heartbeatJob runs a job, pings on success, and reports a failure
with exit code 1 and the failure’s cause as the output. It needs both
SendHeartbeatHttp and ReportHeartbeatHttp, as the full file above provides:
yield* watched(runExport);return yield* HttpServerResponse.json({ done: "export" });A ping that fails or times out is logged and never changes the job’s result. These bindings are tested against the offline mock only; see Compatibility.
Group heartbeats and set quiet hours
Section titled “Group heartbeats and set quiet hours”A HeartbeatGroup holds related
heartbeats and pauses them all with paused: true. A maintenance window keeps
a heartbeat quiet on the days and hours you name:
const jobs = yield* BetterStack.Incidents.HeartbeatGroup("jobs", { name: "Nightly jobs" });yield* BetterStack.Incidents.Heartbeat("export", { name: "Nightly export", period: 86400, grace: 3600, heartbeatGroupId: jobs.id, maintenanceDays: ["sat", "sun"], maintenanceFrom: "00:00:00", maintenanceTo: "06:00:00", maintenanceTimezone: "London",});Removing heartbeatGroupId from the config sends null, but the live API
ignores it: the heartbeat stays in its group. Move it to another group, or
delete the group.
Where next
Section titled “Where next”- Monitors: checks Better Stack runs against your service.
- Watching your services: a heartbeat next to logs, errors and a monitor.
- Reference:
Heartbeat,HeartbeatGroup. - Better Stack: Cron and heartbeat monitors.