(SP: 1) [Stripe Webhook] Add multi-instance claim lock (claimed_at/by…#153
Conversation
…/expires_at, TTL, atomic claim, retry-after)
✅ Deploy Preview for develop-devlovers ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
📝 WalkthroughWalkthroughImplements multi-instance webhook handling for Stripe events using a distributed locking mechanism. Adds claim-based processing to atomically assign webhook events to specific instances, with TTL-based expiration and retry-after logic for busy states. Changes
Sequence Diagram(s)sequenceDiagram
participant Stripe as Stripe API
participant Handler as Webhook Handler
participant DB as Database
participant Logger as Logger
Stripe->>Handler: POST webhook event
Handler->>DB: tryClaimStripeEvent()
DB-->>DB: Check if already processed
alt Already processed
DB-->>Handler: 'already_processed'
Handler-->>Stripe: 200 OK (skip duplicate)
Handler->>Logger: Log duplicate
else Claim owned by another instance
DB-->>Handler: 'busy'
Handler-->>Stripe: 503 Service Unavailable<br/>(Retry-After header)
else Successfully claimed
DB-->>Handler: 'claimed'
Handler->>DB: Business logic (order, metadata)<br/>under claim ownership
alt Processing succeeds
Handler->>DB: Mark processed<br/>(ack event)
DB-->>Handler: Success
Handler-->>Stripe: 200 OK
else Processing fails
Handler->>DB: Clear claimExpiresAt<br/>(release claim early)
Handler-->>Stripe: 503 (retry eligible)
end
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5380a3d258
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| logError('Stripe webhook processing failed', error); | ||
| // P0.8: release claim early so Stripe retries can be claimed immediately. | ||
| try { | ||
| await db | ||
| .update(stripeEvents) |
There was a problem hiding this comment.
Release claim on refund-fullness retry path
The new claim-release logic only runs after the generic logError branch, but the isRefundFullnessUndeterminedError path above returns early with a 500. With the new claim lock, that means claim_expires_at stays in the future and every retry for this specific error will hit the busyRetry path until the 10‑minute TTL elapses, even though the code explicitly wants Stripe to retry promptly. Consider releasing the claim (or setting claim_expires_at to epoch) in the refund‑fullness branch as well so retries can actually be processed.
Useful? React with 👍 / 👎.
…/expires_at, TTL, atomic claim, retry-after)
Description
This PR hardens the Stripe webhook processing path for multi-instance deployments by introducing a durable claim/lock mechanism on
stripe_events. The goal is to prevent duplicate or concurrent processing of the same Stripe event across multiple running instances while preserving safe retry semantics when processing fails.Related Issue
Issue: #<issue_number>
Changes
stripe_events(claimed_at,claim_expires_at,claimed_by) plus an index onclaim_expires_atto support efficient TTL-based claiming.503withRetry-After.processed_atis only set on successful completion.Database Changes (if applicable)
How Has This Been Tested?
Commands (PowerShell):
npx vitest run .\lib\tests\stripe-webhook-contract.test.tsnpx vitest run .\lib\tests\stripe-webhook-mismatch.test.tsnpx vitest run .\lib\tests\stripe-webhook-paid-status-repair.test.tsnpx vitest run .\lib\tests\stripe-webhook-psp-fields.test.tsnpx vitest run .\lib\tests\stripe-webhook-refund-full.test.tsScreenshots (if applicable)
N/A (server-side webhook + DB changes only)
Checklist
Before submitting
Reviewers
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.