fix: paginate weekly digest cron to prevent serverless timeout#2324
Open
Shanidhya01 wants to merge 2 commits into
Open
fix: paginate weekly digest cron to prevent serverless timeout#2324Shanidhya01 wants to merge 2 commits into
Shanidhya01 wants to merge 2 commits into
Conversation
|
@Shanidhya01 is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel. A member of the Team first needs to authorize it. |
GSSoC Label Checklist 🏷️@Priyanshu-byte-coder — please apply the appropriate labels before merging: Difficulty (pick one):
Quality (optional):
Validation (required to score):
|
Member
|
This PR has merge conflicts with the current git fetch origin main
git merge origin/main
# resolve conflicts
git push |
Contributor
Author
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.
Summary
Fixes a scalability issue in the weekly digest cron job where a single unbounded Supabase query loaded all opted-in users into memory at once, causing Vercel serverless timeout failures and partial email delivery at large user counts. Users are now fetched in paginated pages of 50 and processed in parallel sub-batches of 5 via
Promise.allSettled.Closes #1852
Type of Change
What Changed
src/app/api/cron/weekly-digest/route.ts— replaced the single unbounded.select()with awhile (hasMore)pagination loop using Supabase.range(page * PAGE_SIZE, (page + 1) * PAGE_SIZE - 1)PAGE_SIZE = 50constant to control DB fetch size per round-trip (distinct from the existingBATCH_SIZE = 5which controls email concurrency within a page)sentCount→emailsSent,failedCount→emailsFailed; addedtotalUsersProcessedto the JSON response and the completion log lineHow to Test
userstable with more than 50 rows whereweekly_digest_opt_in = trueandemail IS NOT NULL.curl -H "Authorization: Bearer <CRON_SECRET>" http://localhost:3000/api/cron/weekly-digestExpected result:
Response body contains
{ success: true, totalUsersProcessed: N, emailsSent: N, emailsFailed: 0, skippedCount: 0, errors: [] }withtotalUsersProcessedmatching the seeded count. No Vercel timeout. Re-triggering within 6 days skips all users and returnsskippedCount: N.Checklist
console.log, debug code, or commented-out blocksnpm run lintpasses locallynpm run type-check)Additional Context
Two-level batching:
PAGE_SIZE = 50andBATCH_SIZE = 5are intentionally separate constants.PAGE_SIZEcontrols DB memory footprint per query;BATCH_SIZEcontrols Resend API concurrency. Both can be tuned independently without touching logic.Partial-run safety: If a DB error occurs mid-pagination, the handler returns 500, but the
last_digest_sent_atcooldown guard on already-sent users prevents duplicates on the next retry.No breaking changes: Auth flow,
skippedCount,errorsarray shape, and themessage: "No users opted in"response are all preserved.