feat(notifications): dead-letter undeliverable emails with delivery counters - #1792
Open
bilhokista wants to merge 5 commits into
Open
feat(notifications): dead-letter undeliverable emails with delivery counters#1792bilhokista wants to merge 5 commits into
bilhokista wants to merge 5 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1603.
What was already there, and what was not
Retry with capped exponential backoff already exists:
EmailService.deliverEmailWithRetryretries transient failures (network errors, 5xx, 429) withbase * 4^attemptand ±20% jitter, and throws immediately on a 4xx. I did not touch that policy.What was missing is everything after it gives up.
processQueueshifts a message off the in-memory queue, and if delivery fails it catches the error, logs one line, and returns — the message is gone, with nothing left to inspect or replay. There were also no counters.The change
Dead-letter store. New entity
DeadLetteredEmail(dead_lettered_emails) plus migration1788100000000-CreateDeadLetteredEmails.ts. It records the recipient, subject, both rendered bodies, the failure message, the attempt count, and when the message was first queued. The rendered body is stored so a message can be replayed exactly as composed, without re-running template rendering that may since have changed.reasonis an enum with two values, because the operational response differs:permanent— the provider gave a verdict (bad address, revoked key). Resending unchanged will fail again.retries_exhausted— the send never got a verdict. Worth replaying once the provider is healthy.Indexed on
message_id, oncreated_at, and on(reason, created_at)for the obvious triage query: everything worth replaying, newest first.Failure type.
deliverEmailWithRetrynow throwsEmailDeliveryFailure, carrying the reason and attempt count so the caller does not have to re-derive them. It preserves the underlying message, so the existing assertionrejects.toThrow(permanentError)still holds.Counters.
getDeliveryCounters()returns{ sent, retried, deadLettered }by value, ready for a metrics endpoint. Every dead-letter is logged aterrorwith the counters inline.Failure of the failure path.
deadLetter()never rethrows. If the database write fails, it logs that the message is lost and queue processing continues — a dead-letter write must not take down delivery for every message behind it. That case is tested.Tests
Five new cases in
email.service.spec.ts, driving the real queue timer with fake timers:{sent: 1, retried: 1, deadLettered: 0}reason: permanentand the row contents assertedreason: retries_exhausted, attempts: 3processQueuegetDeliveryCounters()hands out a copy, not the live objectBoth existing
TestingModulesetups gained the new repository provider, so the current suite still compiles and runs unchanged.Honest note on verification: I could not run the backend Jest suite locally (no full workspace install). Every changed file was parsed with the TypeScript compiler API, and
email.service.tswas transpiled and executed standalone against a stubbed Nest/TypeORM surface — all 15 behavioural assertions passed, covering the same four scenarios above against the real compiled service rather than a re-implementation. CI is the real check on the Nest-level suite.One thing worth a maintainer's eye: the dead-letter table has no retention policy. For a high-volume outage it will grow. I left that out rather than guessing at a TTL — happy to add a pruning job if you want one.
🤖 Generated with Claude Code
https://claude.ai/code/session_01CrfEY1tvXrbeMDAUzxfuk7