-
Notifications
You must be signed in to change notification settings - Fork 514
[Refactor] Change Retry Logic in Email Sending #1191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6748ed8
feat: db and schema updates for new retry logic
nams1570 87b1c9c
refactor: remove low-level email retry
nams1570 ace7319
refactor: nix unused low-level resend func
nams1570 4897f62
feat: rework email queue step retry logic
nams1570 a9bebf2
fix: skipped, cancelled emails cant be retried now
nams1570 9b3c545
chore: rename failedSendAttempts and backoff number
nams1570 4567fb7
chore: bump number of retries
nams1570 e669c08
refactor: add more info to error handling
nams1570 6ea82d7
fix: retryable emails no longer skip the queuing logic
nams1570 725361e
feat: add new index, constraints on isQueued
nams1570 de35c1c
refactor: split queuing query into two
nams1570 091616a
fix: decouple validation and constraint adding for db
nams1570 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
apps/backend/prisma/migrations/20260210000000_deferred_email_retry/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| -- Add deferred retry fields for email sending | ||
| -- These fields allow the email queue to schedule retries for later iterations | ||
| -- instead of blocking the current iteration with inline retries. | ||
|
|
||
| ALTER TABLE "EmailOutbox" | ||
| ADD COLUMN "sendRetries" INTEGER NOT NULL DEFAULT 0, | ||
| ADD COLUMN "nextSendRetryAt" TIMESTAMP(3), | ||
| ADD COLUMN "sendAttemptErrors" JSONB; | ||
|
|
||
| -- Constraint: nextSendRetryAt can only be set after at least one failed attempt | ||
| -- (if sendRetries is 0, no attempt has failed, so there's nothing to retry) | ||
| -- Use NOT VALID to avoid holding ACCESS EXCLUSIVE lock during full-table validation. | ||
| -- Validation happens in a separate migration to avoid transaction timeout. | ||
| ALTER TABLE "EmailOutbox" | ||
| ADD CONSTRAINT "EmailOutbox_nextSendRetryAt_requires_failure" | ||
| CHECK ("nextSendRetryAt" IS NULL OR "sendRetries" > 0) NOT VALID; | ||
|
|
||
| -- Constraint: sendAttemptErrors can only be set after at least one failed attempt | ||
| ALTER TABLE "EmailOutbox" | ||
| ADD CONSTRAINT "EmailOutbox_sendAttemptErrors_requires_failure" | ||
| CHECK ("sendAttemptErrors" IS NULL OR "sendRetries" > 0) NOT VALID; | ||
|
|
||
| -- Constraint: nextSendRetryAt must be null when email has finished sending | ||
| -- (if finishedSendingAt is set, there's nothing more to retry) | ||
| ALTER TABLE "EmailOutbox" | ||
| ADD CONSTRAINT "EmailOutbox_no_retry_after_finished" | ||
| CHECK ("finishedSendingAt" IS NULL OR "nextSendRetryAt" IS NULL) NOT VALID; |
7 changes: 7 additions & 0 deletions
7
apps/backend/prisma/migrations/20260210000001_deferred_email_retry_validate/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| -- Validate the deferred retry constraints added in the previous migration. | ||
| -- This runs in a separate transaction to avoid timeout, and only takes | ||
| -- SHARE UPDATE EXCLUSIVE lock (allows concurrent reads/writes). | ||
|
|
||
| ALTER TABLE "EmailOutbox" VALIDATE CONSTRAINT "EmailOutbox_nextSendRetryAt_requires_failure"; | ||
| ALTER TABLE "EmailOutbox" VALIDATE CONSTRAINT "EmailOutbox_sendAttemptErrors_requires_failure"; | ||
| ALTER TABLE "EmailOutbox" VALIDATE CONSTRAINT "EmailOutbox_no_retry_after_finished"; |
6 changes: 6 additions & 0 deletions
6
apps/backend/prisma/migrations/20260213004424_email_outbox_is_queued_index/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| -- SPLIT_STATEMENT_SENTINEL | ||
| -- SINGLE_STATEMENT_SENTINEL | ||
| -- RUN_OUTSIDE_TRANSACTION_SENTINEL | ||
| -- Index on isQueued for efficient queueReadyEmails() queries. | ||
| -- Most emails have isQueued=TRUE (already processed), so filtering for FALSE is highly selective. | ||
| CREATE INDEX CONCURRENTLY IF NOT EXISTS "EmailOutbox_isQueued_idx" ON /* SCHEMA_NAME_SENTINEL */."EmailOutbox" ("isQueued"); |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.