Skip to content

Email on Cloudflare: Phase 2 (production sending prepared) - #1923

Merged
neuromechanist merged 2 commits into
devfrom
feature/issue-1893-epic-email-cf
Oct 24, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/issue-1893-epic-email-cf

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Phase 2 of epic #1893. Production still sends through Resend after this merge; the switch is a later config PR, after the Cloudflare limit increase and a clean staging soak.

  • Phase 2a: Resend fallback and production binding #1917: sendEmail falls back to Resend once, only when Cloudflare refuses with a rate limit, daily limit, account pause or unavailable sending domain, gated by EMAIL_FALLBACK=resend (production only, set in the flip PR). Production gets the send_email binding restricted to noreply@ and [email protected].
  • Phase 2b+2c: announcement queue and delivery events #1916: announcements go through a Queue when the Cloudflare provider is active (staging now), with per-recipient idempotent outcomes, bounded retries and nemar admin notify --status; bounce and complaint events from Cloudflare are stored in D1 for 180 days. Migrations 0100 and 0101.

Already provisioned: the outbox and events queues with dead-letter queues (production and staging), event subscriptions for test.nemar.org and nemar.org, and nemar.org onboarded for Email Sending (only cf-bounce records added; apex records unchanged).

Deploy note: the first dev deploy with queue bindings needs Queues permission on the CI Cloudflare token; if it fails with code 10000, that permission is missing.

Part of #1895, epic #1893.

Test plan

  • Each phase PR: CI green, Sonnet review (2a one round; 2b two rounds; 2c one round), all findings fixed
  • Staging: a queued nemar admin notify --user announcement arrives and --status reaches done
  • Staging: sign-in still works

🤖 Generated with Claude Code

* Fall back to Resend on Cloudflare quota refusal

* Bind Cloudflare email in production, no switch

* Sort imports in email-provider test

* Log and harden the Resend fallback
* feat: queue announcements on the outbox

Migration 0100 adds status and outcome counters to broadcast_emails. sendBroadcast enqueues one message per allowed recipient when the Cloudflare provider and EMAIL_OUTBOX are both present. Tested with real D1 and a recording Queue binding.

* feat: consume the announcement outbox

queue() dispatches broadcast messages to handleOutboxBatch, which sends through sendEmail and records each outcome on the audit row. Rate limits retry after 300s; unknown outcomes are never retried. Exports CLOUDFLARE_LIMIT_CODES.

* feat: announcement status route and CLI

GET /admin/notify/:id reports a queued announcement's counts; nemar admin notify prints the queued result and gains --status. POST answers 502 when enqueueing fails.

* fix: idempotent announcement outcomes

Add broadcast_outcomes keyed by recipient hash; counters move only when the outcome insert took. A partial enqueue records unqueued outcomes and answers 200 with the counts.

* fix: bound outbox retries and pace limits

max_retries 10 with growing limit delay, give up and record a failure on the final attempt, defer the rest of a batch after a limit refusal, skip already-recorded recipients. Names the queues in wrangler and tags the unrecognised-message log.

* fix: show enqueue failure and redaction in CLI

* test: allow 0100's outcomes table after 0098

* fix: dedupe queued recipients by hash key

* test: pin getBroadcastStatus export

Adds the new CLI API export to the per-module pin and the post-split list. Tested: api export surface, notify CLI and route inventory tests.

* feat: store email bounce and complaint events

* feat: route email events, prune after 180 days

* docs: amend ADR 0110 for the soak and events

* test: account for 0101 and events consumers

The outbox retry check now reads only the outbox consumers, and the 0098 schema check lists 0101's table and indexes as a later migration's. Tested: events, outbox, 0098 migration and provider suites.

* fix: delay event retries, tighten ADR 0110

A failed D1 write now retries after a growing delay instead of at once; the ADR states the outbox retry bound, the de-duplication, and that every other event type is dropped. Tested: events and outbox suites, ADR index, typecheck.
@neuromechanist
neuromechanist merged commit 6b67a0a into dev Oct 24, 2026
27 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1893-epic-email-cf branch October 24, 2026 08:44

This branch was previously deployed

1 inactive deployment
npm — 6ba0f005 Deployed Oct 24, 2026 by neuromechanist via publish #3842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant