Rebasely Beta: data may be purged

examplesstorefront Public

Keep expiring orders behind ones left to the webhook #3

  1. Justin Torre opened this

    The expiry cron could stop cancelling abandoned checkouts entirely, and they kept their stock until someone noticed.

    Why

    Every five minutes expireOrders asked D1 for the 50 oldest orders still pending_payment past the 30 minute window. For each one it tries to cancel the PaymentIntent, and when Stripe refuses because the payment already went through, it leaves the order for the webhook to mark paid. That order stays pending_payment, and it stays the oldest.

    So if the webhook falls behind (a bad deploy of the signing secret, say), paid orders pile up at the front of the query. Once there are 50 of them, every run picks the same 50, leaves all of them to the webhook, and never reaches the abandoned checkouts behind them. Those keep their stock, and products show as sold out while nobody is buying them. It clears up only when Stripe's retries finally get through.

    What changed

    • src/expiry.ts pages through the expired orders 50 at a time by (created_at, id), starting each page after the last order it looked at, instead of asking for the oldest 50 again.
    • A run looks at no more than 200 orders (MAX_PER_RUN), so its calls to Stripe stay bounded. The next run starts from the oldest again.
    • The checkout concept explains the cursor and the cap.

    No schema change. The query uses the existing orders_by_status index on (status, created_at).

    Testing

    • New regression test: 51 expired orders where the oldest 50 have intents Stripe will not cancel. The 51st is cancelled and its stock comes back. On main this test fails: the report lists the 50 and nothing is cancelled.
    • New test that one run stops at MAX_PER_RUN and the next run picks up the rest.
    • npm test (51 tests) and npm run typecheck pass. kb check and arch check pass.
  2. src/expiry.ts line 13
    13export const MAX_PER_RUN = 200;
    1. Justin Torre

      A note for later: if an outage ever leaves more than 200 paid orders pending, we are back to the same problem with a bigger number. That is a lot of stuck payments for a shop this size, so I am fine with the cap. If it does happen, the next step is a last_checked_at column so a run skips orders it looked at in the past hour.

    1. Justin Torre

      Checked the plan for the new query against a local copy of the schema: it still uses orders_by_status, and the row value comparison is the same form the catalog cursor already uses on D1.

  3. Justin Torre pushed to expire-behind-stuck-orders 8b37e66 → 38a9ec4

Open

Ready to merge

  • Successduragit/ci/testsucceededDetails
  • No conflictsmain can fast-forward to the head

Sign in to comment.