Skip to content
betterstack-alchemyUnofficial

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.

BoxCloud.Server
WorkerCloud.Service
my-teamBetter Stack team
WorkerAliveheartbeat
period
60 s
grace
60 s
  • Workerpings url →WorkerAlive
The worker requests the heartbeat's URL every minute. If a request is more than period plus grace late, Better Stack opens an incident.

A nightly backup that must report once a day, with two hours of slack:

alchemy.run.ts
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.

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:

alchemy.run.ts
return { backupUrl: backup.url };

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:

src/worker.ts
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),
);
}),
) {}

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:

src/jobs.ts
const alive = yield* BetterStack.Incidents.SendHeartbeat(ExportDone);
const watched = yield* BetterStack.Incidents.heartbeatJob(ExportDone);

Then call it where the work finishes:

src/jobs.ts
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.

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:

src/jobs.ts
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.

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:

alchemy.run.ts
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.