fabbr@site:~/projects/magellan/cloud$

cloud

// ~/projects/magellan/cloud.ts
const cloud = {
  status: "active",
  started: "2026-09-19",
  tags: ["typescript", "cloudflare", "d1", "r2"],
  repo: "https://github.com/fabbrito/magellan-cloud",
};

magellan-cloud is two Cloudflare Workers. The ingest-worker takes the batches magellan-device uploads, commits their readings to D1 and archives every raw batch in R2. The api-worker answers read queries from clients, today Grafana, and writes nothing. Both run on Cloudflare’s free tier: at a few hundred D1 rows a day against a free limit of 100,000, I see no reason to leave it, and the design is loose enough to keep fitting it.

rebuilddeviceingestworkerR2 archiveD1 readingsAPI workerGrafana

The registry holds whatever a device’s manifest declares: its sources, their metrics, and each metric’s kind, unit and exponent. Its tables name no inverter, so a new kind of source needs no change here. The changes this repository did need came from the contract instead: the heartbeat, manifest hashing and how manifests are stored.

How a batch lands

Each device holds its own token, and the ingest-worker stores only the token’s hash; the token is the authority, and a path naming a different device is refused with a 403. After the token, the body is parsed against the contract, and the raw batch goes to R2 before anything is committed to D1. Then the readings commit in one write, so either the whole batch lands or none of it does. R2 keeps every batch and every manifest exactly as sent, and D1 is an index over that archive that could be rebuilt from it.

The device retries until it hears back, so the same batch can arrive twice. Each reading is keyed by device, source and timestamp, so a replayed batch collides with itself row by row and nothing is stored twice. So far that is design rather than observation: watching the traces and logs Cloudflare keeps, I haven’t seen a replay collide yet. A batch that names a manifest the cloud hasn’t stored is archived and answered with a 5xx, so the device keeps it and tries again once the manifest is in.

How it got here

For the first week a third Worker, the app-worker, also served a single-page dashboard behind Cloudflare Access. On 2026-09-25 I dropped it: I was reinventing tools that already existed and were battle tested, and Grafana was one I knew. That was also when I decided the project should be headless, a proxy between devices and whatever reads them, with the client living elsewhere. The app-worker became the api-worker, serving data and device health only.

The second cut came on 2026-09-30. A jobs-worker was planned to compute rollups on a schedule, but once real data was coming in the volume was small enough for D1 to aggregate each rollup as it is read, so the jobs-worker moved to the roadmap.

How the code is written

Archiving before committing is one case of the first goal in this repository’s adaptation of TigerStyle: a correct record first, then cost, then developer experience. Where it departs from TigerStyle is the process: a Worker has none that lives on, so a thrown error fails one request and restarts nothing. That is why assertions live only behind the parser. An assertion a valid request can reach turns a bug into a retry, and a retry the device never stops making ends as a batch its buffer eventually drops.

Decisions

  • Heartbeats are stored in D1 but not archived: they describe the device’s state right now, which nothing needs to rebuild later.
  • Nothing runs on a schedule. Deciding a device is silent is the client’s job; Grafana has the alert rule for it, though no notification channel is wired yet.
  • A failure always lands on a response class the device acts on: committed, rejected, or try again later.

What’s next

Two pieces exist only on paper. The rebuild of D1 from R2 has never run, since I haven’t needed it, and the jobs-worker waits until the volume makes aggregating on read too slow.