Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,36 @@ jobs:
secrets.WAKER_SIGNING_KEY = wakerKey;
console.log("WAKER_SIGNING_KEY: configured");
} else {
console.log("WAKER_SIGNING_KEY: not configured (native cron only)");
console.log("WAKER_SIGNING_KEY: not configured");
}

// Interlock: once wrangler.toml has no [triggers], the waker task is
// the only schedule source. Losing the key as well would silently
// stop every hourly sweep, so fail the deploy instead.
// Interlock: a waker key and a native Cron Trigger are the only two
// schedule sources. `wrangler.toml` ships `[triggers] crons = []`
// (explicitly empty, which is what clears the deployed Cron
// Trigger), so losing the key as well would silently stop every
// hourly sweep — fail the deploy instead. Only a non-empty crons
// list counts: an absent or commented-out key sends no /schedules
// update at all (Cloudflare keeps the existing trigger).
const wranglerToml = fs.readFileSync("wrangler.toml", "utf8");
if (!wakerConfigured && !/^\[triggers\]/m.test(wranglerToml)) {
const triggersHeader = wranglerToml.match(/^\[triggers\]\s*$/m);
const triggersBody = triggersHeader
? wranglerToml
.slice(triggersHeader.index + triggersHeader[0].length)
.split(/^\[/m)[0]
: "";
const cronsList = triggersBody.match(/^crons\s*=\s*\[([\s\S]*?)\]/m);
// A comment inside the array is not an entry: strip comments before
// deciding whether the list actually holds a cron expression.
const cronsEntries = cronsList
? cronsList[1].replace(/#[^\n]*/g, "")
: "";
const cronsConfigured = /[^,\s]/.test(cronsEntries);
console.log(
`native cron: ${cronsConfigured ? "configured" : "disabled"}`,
);
if (!wakerConfigured && !cronsConfigured) {
console.error(
"neither [triggers] nor WAKER_SIGNING_KEY is configured: nothing would run",
"neither a Cron Trigger (crons) nor WAKER_SIGNING_KEY is configured: nothing would run",
);
process.exit(1);
}
Expand Down
6 changes: 3 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ The worker serves the frontend's static assets from `web/dist/` via the `ASSETS`

`services/scheduler.ts` is the single business implementation. Both schedule entries go through `services/scheduler-tick.ts` `runSchedulerTick(env, { source, scheduledFor })`:

- **native entry**: `scheduled()` in `index.ts`, `source: "cron"`, `scheduledFor: event.scheduledTime` (awaited, not `waitUntil`);
- **native entry**: `scheduled()` in `index.ts`, `source: "cron"`, `scheduledFor: event.scheduledTime` (awaited, not `waitUntil`). Not scheduled in production any more — `worker/wrangler.toml` ships `crons = []`; it stays in code for the rollback path;
- **waker entry**: `services/waker-callback.ts`, job `hourly-reminder-sweep`, `source: "waker"`, `scheduledFor: ctx.scheduledFor`; mounted at `/_waker` by `routes/waker.ts` using the verbatim SDK copy in `worker/src/waker/` (waker-client 2.0.0 — never hand-edit that directory).

The guard in front of the business pass is one atomic D1 claim per UTC hour (`{prefix}scheduler_tick_claims`, `INSERT … ON CONFLICT(tick_key) DO NOTHING` + `meta.changes`), so cron and waker can never sweep the same hour twice; the SDK's own `{prefix}waker_executions` ledger dedupes executionId retries. Failure semantics: cron keeps the claim on failure (no re-run that could double-send), waker releases it and rethrows so the platform retry (same executionId) completes the hour. Claims are pruned after 7 days.

Two cases go beyond the plain table: manual triggers and dead-letter replays arrive with `trigger: "manual"` and `scheduledFor` = the dispatch time, so they run the sweep **without taking the hourly slot** (the executionId ledger and KV dedupe keep them idempotent) — otherwise the console's "run now" would be swallowed by the cron claim of that hour. And when the platform aborts an execution (timeout / evicted isolate) the claim is never released, so a waker retry may take over its own orphaned claim after `WAKER_STALE_CLAIM_SECONDS` (20s, smaller than `backoffSeconds` 30) and finish the hour; a cron claim is never taken over.

Waker is optional and the **only** setting is `WAKER_SIGNING_KEY` (GitHub Secret → `wrangler deploy --secrets-file`): unset/blank/`*_PLACEHOLDER` means native cron only. The gate is `wakerSigningKey()` in `services/scheduler-tick.ts`; the receiver stays mounted and fails closed with 500 JSON `signing_key_missing`. Do not reintroduce `WAKER_MODE`/`WAKER_URL`/project tokens — the SDK's `mountWakerReceiver`, `createWakerClient*`, `syncManifest` and `resolveWakerMode` must stay uncalled.
The hourly sweep is driven by the waker job alone. `worker/wrangler.toml` keeps `[triggers]` with `crons = []` — an explicit empty array, because per Cloudflare docs removing or commenting out the `crons` key does **not** disable an existing Cron Trigger (wrangler only PUTs `/schedules` when `crons` is defined), so deleting the section would leave the old cron running. The **only** setting is `WAKER_SIGNING_KEY` (GitHub Secret → `wrangler deploy --secrets-file`): unset/blank/`*_PLACEHOLDER` means unconfigured, and `deploy.yml` then fails the deploy because no non-empty `crons` exists either. The gate is `wakerSigningKey()` in `services/scheduler-tick.ts`; the receiver stays mounted and fails closed with 500 JSON `signing_key_missing`. Do not reintroduce `WAKER_MODE`/`WAKER_URL`/project tokens — the SDK's `mountWakerReceiver`, `createWakerClient*`, `syncManifest` and `resolveWakerMode` must stay uncalled.

**Cloudflare bindings**:

- `DB` — D1 database
- `KV` — KV namespace (used for scheduler deduplication and status)
- `ASSETS` — static file serving
- Secrets: `JWT_SECRET`, `SETUP_SECRET`, and optional `WAKER_SIGNING_KEY` (empty ⇒ native cron only)
- Secrets: `JWT_SECRET`, `SETUP_SECRET`, and `WAKER_SIGNING_KEY` (required in production — the hourly sweep's only entry point; empty ⇒ `deploy.yml` fails unless `crons` is non-empty)
- Env var: `TABLE_PREFIX` (three states: unset → default `enotify`, tables like `enotify_users`; custom value → verbatim with trailing `_`; explicit `""`/CI `none` → no prefix; allows shared DB with prefix isolation)

### Frontend (`web/`)
Expand Down
24 changes: 13 additions & 11 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ In your repository Settings → Secrets and variables → Actions, add:
| `CLOUDFLARE_API_TOKEN` | Cloudflare API Token (needs Workers and D1 permissions) |
| `JWT_SECRET` | Any random 64-char string — e.g. `openssl rand -hex 32` |
| `SETUP_SECRET` | Any random string — e.g. `openssl rand -hex 16` |
| `WAKER_SIGNING_KEY` | **Optional** waker callback signing key; leave unset to stay on native cron (see "Waker Scheduling") |
| `WAKER_SIGNING_KEY` | **Required** waker callback signing key — the only schedule entry for the hourly sweep (see "Waker Scheduling") |

In Settings → Secrets and variables → Actions → Variables, add:

Expand Down Expand Up @@ -186,19 +186,21 @@ CI deployment convention (`.github/workflows/deploy.yml`): a GitHub Variables `T
| `JWT_SECRET` | JWT signing secret, any random 64-char string |
| `SETUP_SECRET` | Database initialization route secret, any random string |
| `TABLE_PREFIX` | Table prefix, three states: unset → default `enotify` (tables like `enotify_users`); explicit empty string / CI `none` → no prefix; custom value → verbatim with trailing `_` |
| `WAKER_SIGNING_KEY` | Optional. Waker callback signing key; missing / blank / `*_PLACEHOLDER` means unconfigured → native cron only |
| `WAKER_SIGNING_KEY` | **Required.** Waker callback signing key (the only schedule entry for the hourly sweep); missing / blank / `*_PLACEHOLDER` means unconfigured, and the CI interlock then fails the deploy |

## Waker Scheduling (optional)
## Waker Scheduling

By default the hourly reminder sweep runs on the native Cloudflare Cron Trigger (`[triggers]` in `worker/wrangler.toml`, `0 * * * *` UTC). It can also be driven by the [waker](https://github.com/WorkerHub/waker) platform: **the only setting is `WAKER_SIGNING_KEY`** — leave it unset and the default cron keeps running; set it and both entries coexist, arbitrated by an hourly claim, so nothing is sent twice.
The hourly reminder sweep is driven by the `hourly-reminder-sweep` job on the [waker](https://github.com/WorkerHub/waker) platform, and **the only setting is `WAKER_SIGNING_KEY`**. `worker/wrangler.toml` has no native Cron Trigger (`[triggers]` stays with `crons = []`), so Cloudflare runs no cron and the cron slot is free; without this key nothing would run at the top of the hour, so the CI interlock fails the deploy when it is missing.

> Why `crons = []` instead of deleting the `[triggers]` section: per the [Cloudflare docs](https://developers.cloudflare.com/workers/wrangler/configuration/#triggers), commenting out or removing the `crons` key does **not** disable an existing Cron Trigger — only an explicit empty array does (wrangler only PUTs `/schedules` when `crons` is defined).

> Signature verification, the timestamp window and executionId idempotency come from the official SDK source copy (`worker/src/waker/`, `@workerhub/waker-client` 2.0.0). That directory must not be edited by hand.

### 1. Configure the signing key (optional)
### 1. Configure the signing key (required)

Add the GitHub Secret `WAKER_SIGNING_KEY` (value = the signing key the waker console shows **once** when the project is created; keep a copy in your password manager first). At deploy time it is only written to `$RUNNER_TEMP/secrets.json` (0600) and uploaded via `wrangler deploy --secrets-file`; CI logs only `configured` / `not configured`, **never the value**.

Empty, whitespace-only, `undefined` and `*_PLACEHOLDER` values are all treated as "not configured". In that state the receiver is still mounted but fails closed: a real callback gets **500 JSON `signing_key_missing`**, a request without signature headers gets 400, and `GET /_waker/execute` gets 404 — it **never falls through to the SPA's 200 HTML**, never queries the DB, writes the ledger or runs business code. Deploying without the key always succeeds.
Empty, whitespace-only, `undefined` and `*_PLACEHOLDER` values are all treated as "not configured". In that state the receiver is still mounted but fails closed: a real callback gets **500 JSON `signing_key_missing`**, a request without signature headers gets 400, and `GET /_waker/execute` gets 404 — it **never falls through to the SPA's 200 HTML**, never queries the DB, writes the ledger or runs business code. With no key and an empty `crons`, the CI interlock fails the deploy instead (otherwise nothing would run at the top of the hour); a local `pnpm deploy` bypasses that interlock, so confirm the secret is set before deploying.

### 2. Create the job in the waker console

Expand All @@ -223,13 +225,13 @@ There is no registration client, project token, dynamic registration/cancellatio

### 3. Database migration

`schema_v3` adds two tables under the configured table prefix (default `enotify_waker_executions`, `enotify_scheduler_tick_claims`): the SDK's executionId idempotency ledger and the hourly claim table. After upgrading, call `POST /api/setup` once (or use "check and upgrade database" in the admin UI); migrations are additive and re-runnable. If the key is configured while the claim table does not exist yet, **both** entries stop safely (no double-send wins), so migrate before configuring the key.
`schema_v3` adds two tables under the configured table prefix (default `enotify_waker_executions`, `enotify_scheduler_tick_claims`): the SDK's executionId idempotency ledger and the hourly claim table. After upgrading, call `POST /api/setup` once (or use "check and upgrade database" in the admin UI); migrations are additive and re-runnable. If the key is configured while the claim table does not exist yet, the waker entry stops safely (the claim runs before any send, so no double-send wins), so migrate before configuring the key.

### 4. Dual run, cutover and disabling
### 4. Current state, rollback and disabling

- **Dual run**: while `[triggers]` stays, both entries exist. Each UTC hour exactly one of them wins the D1 claim and runs the sweep; the other one skips (the waker side answers `duplicate_tick`), so no notification or renewal happens twice. Claim rows are kept for 7 days for troubleshooting.
- **Cut over to waker only**: first confirm exactly one claim row per hour (`SELECT tick_key, source, claimed_at FROM enotify_scheduler_tick_claims ORDER BY claimed_at DESC LIMIT 6`), then delete `[triggers]` from `worker/wrangler.toml` and deploy. CI interlocks: deploying with no `[triggers]` and no `WAKER_SIGNING_KEY` fails outright (otherwise nothing would run at the top of the hour).
- **Disable callbacks**: `wrangler deploy --secrets-file` is additive (omitting a key does not delete the old value). Restore `[triggers]`, deploy, and explicitly run `npx wrangler secret delete WAKER_SIGNING_KEY`. Even if you forget, the hourly claim still prevents duplicate runs.
- **Current state: waker only**: `worker/wrangler.toml` keeps `[triggers]` but with `crons = []` (an explicit empty array is what actually clears the Cron Trigger on Cloudflare's side), so the hourly sweep is driven by the waker job `hourly-reminder-sweep` alone.
- **Roll back to dual run / cron only**: set `crons` back to `["0 * * * *"]` in `worker/wrangler.toml` and deploy. While both entries exist, each UTC hour exactly one of them wins the D1 claim and runs the sweep; the other one skips (the waker side answers `duplicate_tick`), so no notification or renewal happens twice. Claim rows are kept for 7 days for troubleshooting.
- **Disable callbacks**: `wrangler deploy --secrets-file` is additive (omitting a key does not delete the old value). Restore `crons = ["0 * * * *"]` and deploy first, then explicitly run `npx wrangler secret delete WAKER_SIGNING_KEY`. Even if you forget, the hourly claim still prevents duplicate runs.
- **Rotate the key**: rotate in the waker console → the old key stops working immediately → update the GitHub Secret `WAKER_SIGNING_KEY` → redeploy. Callbacks during the window are rejected (401) and retried by the platform.
- **Manual trigger / dead-letter replay**: the platform sends the **dispatch time** for these runs, not a nominal slot (waker `manualTrigger` / `replayDeadLetter`), so they run the sweep **without taking the hourly claim**; the executionId ledger and the sweep's own KV dedupe keep them from sending anything twice. The console's "run now" therefore really runs (response `trigger: "manual"`, `executed: true` in the execution log) instead of being swallowed by the cron claim of that hour.
- **Timeout / crash**: when the platform aborts a request after `timeoutSeconds` there is no chance to release the claim. A retry (same executionId) takes over the SDK ledger row and the `source=waker` hourly claim after 20 seconds and finishes the hour; a cron claim is **never** taken over (cron has no retry, and re-running would send twice).
Expand Down
Loading
Loading