DEV Community

Cover image for Cloudflare Worker dead-man switch on the free plan: the cron didn't fire
Christian Anderson
Christian Anderson

Posted on

Cloudflare Worker dead-man switch on the free plan: the cron didn't fire

Everything I've got that watches my homelab lives in my homelab. Uptime Kuma, my agents, the little ESP32 watchdog board. That's fine until the power goes or the internet drops. Then all of it goes quiet at the same moment, and quiet is exactly what "everything's fine" looks like from my phone.

So on 13 October 2026 I built a dead-man switch that lives somewhere else. My boxes check in every minute. If the check-ins stop, something off-site notices and posts to Discord. It runs on Cloudflare's free plan.

How it's put together

Two Proxmox nodes and an ESP32 board POST every 60 seconds to a Cloudflare Worker, which hands off to a SQLite Durable Object with a self-rearming 60 second alarm, which posts to a Discord webhook

  • Senders: both my Proxmox nodes run a systemd timer that does a curl POST every 60 seconds. Since 16 October my ESP32 watchdog board pings it too, every check cycle.
  • The Worker checks a secret path segment and a bearer token. If either is wrong it returns a plain 404, so from outside it looks like nothing's there. It also 404s any check name it doesn't know, so a typo can't quietly create a new check.
  • A Durable Object holds the state: one SQLite row per check, with last seen time and up/down state.
  • The grace period is 300 seconds per check.
  • Discord messages: ⚠️ when a check stops, ✅ when it comes back (with how long it was silent), and 🚨 "Home offline?" if every check goes down at once. That last one is the power cut or internet case.

Why a Durable Object and not KV

Honestly, because of my API token. The Cloudflare token I deploy with can edit Worker scripts but can't do KV or D1. A SQLite-backed Durable Object only needs the scripts permission, so it just worked. It's also a better fit: one object, one small table, and state changes happen in one place.

Usage is tiny. About 4.5k requests a day against the free plan's 100k.

The bit that didn't work: the cron never fired

My first version was the obvious design. Worker for check-ins, plus a cron trigger (* * * * *) that runs every minute and sweeps for anything that's gone quiet too long.

First test, I paused the pings from one node. It went silent for 414 seconds, well past the 300 second grace. No alert.

I looked at the Worker's invocations through Cloudflare's GraphQL analytics. There were check-in invocations and nothing else. The scheduled handler hadn't run at all. For the first 10 minutes or so after deploying, the cron trigger simply didn't fire.

I don't know why, and I didn't need to. What it told me was more useful: if a dead-man switch's own timer can just not run, then the switch is only as good as that timer. And a cron trigger isn't something I can see or test properly from my side.

The fix: the object wakes itself up

Durable Objects have alarms. You set a time, and the object's alarm() method gets called then. So now the object keeps its own clock:

// Self-rearming 60 s alarm: the sweep keeps running even when every check-in has stopped.
// The cron trigger is only a backup that re-creates the alarm if it was ever lost.
async arm() {
  if ((await this.ctx.storage.getAlarm()) === null) await this.ctx.storage.setAlarm(Date.now() + 60000);
}

async alarm() {
  await this.ctx.storage.setAlarm(Date.now() + 60000);
  await this.sweep();
}
Enter fullscreen mode Exit fullscreen mode

Every ping and every sweep calls arm(), so there's always an alarm set. The alarm handler sets the next one first, then sweeps. The cron trigger is still there, but now it's just a backup. All it does is call the sweep, and the sweep re-arms the alarm if it ever went missing.

Two other small things in the code I'm glad I did:

  • A check that has never been seen still alerts. On its first sweep a new check gets its clock started, so if it never arrives at all, it goes down after the grace period like anything else.
  • The secret comparisons are constant-time. Probably overkill for a homelab, but it's six lines.

Testing it properly this time

I paused the same node's pings again. The ⚠️ alert came at about 6 minutes, which is what you'd expect from a 5 minute grace and a sweep every minute. When I let the pings resume, the ✅ recovery message arrived 4 seconds later, because recovery is handled on the ping itself, not on the next sweep.

Here's what the switch thinks right now, from my deploy script's status command:

Status output: three checks, each with a 300 second grace, all up, last seen 46, 57 and 36 seconds ago

One nice side effect of using a webhook

On 16 October I wanted these alerts in a different Discord channel. A webhook's channel can be changed with a PATCH, and the webhook URL stays the same. So I moved it without touching the Worker at all. No redeploy.

What I'd do differently

  • Don't trust a scheduler you can't watch. If the whole point is "notice when nothing happens", the thing doing the noticing needs its own heartbeat. The alarm gives me that.
  • Test the silence, not the noise. My first test was the right one: stop a sender and wait. It's the only test that matters for a dead-man switch.
  • Make the "everything is down" message different. One box going quiet and the whole house going quiet are different problems. The 🚨 message tells me which one it is before I've even opened anything.
  • Keep it boring. No dependencies, one Worker file, about 120 lines.

I wrote about the ESP32 watchdog board separately. Between the two, the board tells me when a service is down, and this tells me when the board can't.

What do you use for the "the whole house is offline" case? I'd love to know if anyone's doing it a simpler way.


🤖 Drafted with AI assistance from my own homelab notes, logs and repos, and fact-checked against them before publishing.

Top comments (0)