Repository navigation
perf(tracking): start the first run after a payment at once, and fill its pages as answers arrive - #1773
Merged
Conversation
The first run after a payment only started when the Stripe return page loaded. A person who closed the tab before it did had the prompts saved in onboarding wait for the nightly run, up to a day. The return page and the checkout.session.completed webhook now share one helper (web/src/lib/billing/first-run.ts) that starts the first run for the organization's first brand, after the plan alignment, and only for an active or trialing subscription. The API keeps it to one run per checkout (server/src/lib/first-run.js): calls for a brand are taken one at a time, and one that finds a full run already waiting or running, or one started since the checkout began, gets that run's id back with created: false. The return page carries that id to the dashboard as before, and only the caller that started the run also starts the volume analysis. A failure in the webhook is logged and Stripe still gets its 200. Deploy the API before the web app: an older API starts a run for every call, so the two callers would start two. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ne run per purchase Review follow-ups to the first run after a payment: - A delayed payment (SEPA, for one) leaves the checkout's subscription incomplete, so nothing started the first run. The webhook now starts it on customer.subscription.updated when the status was incomplete, since the subscription's creation; never on renewals or other changes. - The volume analysis starts in the API, with the run it creates (routes/internal-trigger-tracking.js, moved out of server.js and tested), not from the web app, so a caller that gives up waiting still gets it. The analyze-volumes endpoint had no other caller and is gone. - The jobs read for an existing run go back to the purchase when it is older than 20 hours, so a return page opened again a day later finds the finished run instead of starting a second one. - The first brand is the first active one: a paused brand is skipped. - The job's flag is afterPayment, not firstRun. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first run after a payment is the customer's first look at the product, and three things kept it slow: - The job queue had no order. A first run started during the nightly cycle waited for a free slot like any other job, and each nightly run holds its slot through a Cloro drain that can last an hour. The first run (firstRun in the job's data) now goes ahead: it may take one slot beyond TRACKING_CONCURRENCY (TRACKING_PRIORITY_SLOTS, default 1), checks for a slot every second, and regular jobs step aside while one waits. The recovery sweep keeps that place. - The daily rollups take a run's days only once it stamps, so until then Citations, Mentions and Visibility's 7/30/90-day ranges showed nothing for a brand whose first run it is. A brand's first full run now refreshes its days as answers land, at most every 2 minutes and one refresh at a time, then once more before the stamp, which waits for it. Later runs are unchanged: their mid-run counts still never show. - A first run that ends short of complete checked for its stamp every 2 minutes; the stamp is what fills the Action Center. It now checks every 30 seconds. No change to what is sent to Cloro or how often. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e answers Review follow-ups to the first run's progressive rollups and queue place: - A first run's refreshes are at least max(2 min, 5 × the last refresh's duration) apart, so a brand whose refresh is slow does not refresh back to back. - The stamp now waits for a refresh still running itself (run-settlement.js, given the refresher). A run that stamps at once needs no refresh of its own, as the stamp's covers it; one that has to wait for late answers refreshes once when the wait starts, then on the late polls as answers land, so they show before the stamp. - The comment on why refreshes go one at a time: the SQL functions already take a per-brand advisory lock; starting them one at a time keeps refreshes from sitting on connections while they wait for it. - The job flag that orders the queue is afterPayment; the worker's firstRun (no run of the brand ever stamped) says how it differs. - Tests: the run after a payment keeps its extra slot on a retry, the trigger endpoint asks for priority, the stamp waits for the refresher, and the worker hands a first run's refresher and 30 s poll to the stamp (tracking-worker.first-run.test.js). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 29, 2026
Stripe doesn't order its events, so the subscription's incomplete to active update can come before the checkout's, whose alignment then hasn't run. Align here too before the first run starts, so it tracks only the plan's engines.
gkhngyk
approved these changes
Oct 29, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
Looks good. Branch take-1773 (613718b) is this PR's head 9e84651 merged with take-1772, so the stack carries #1772's fix. There is nothing to change in this PR's own diff.
Checked
- Priority slot: only the job that
/api/internal/trigger-trackingcreated getspriority, and recovery keeps it bydata.afterPayment. A reused run (created:false) isn't started again, so the extra slot can't run a purchase twice. The slot is taken before the first await, as before. The total is capped atTRACKING_CONCURRENCY + TRACKING_PRIORITY_SLOTS. - No extra provider calls: the run submits the same tasks; only the queue order changes. The progressive rollups are DB refreshes (paced at max(2 min, 5 × last), one at a time, and the stamp waits for one in flight). The 30 s late poll is two counts per poll.
- Embeds: none (
tracking_runsreadsid;listWaitingJobsaddsdata). - Migrations: none. Read-only gate: no web change in this part.
Follow-ups (not blocking)
isFirstRunmeans "no run of this brand ever stamped". A brand whose runs never stamp keeps the progressive refreshes on every run. That costs DB time only.- A priority job cancelled while it waits for a slot keeps the other jobs stepping aside until it gets a slot and its claim fails. With the extra slot it rarely waits at all.
Deploy: API only; TRACKING_PRIORITY_SLOTS is optional (default 1).
Tests: at the stack tip merged with main: job-runner, first-run-rollups, run-settlement and workers (100 tests) pass. Full server suite as in #1772 (only framer-api and the known timing failures). prettier is clean.
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
The first tracking run after a purchase now starts in seconds even during the nightly batch, and fills the pages as answers arrive. It sends no extra calls to Cloro.
Where the time went:
Changes
afterPayment) may use one extra slot (TRACKING_PRIORITY_SLOTS, default 1, documented inserver/.env.example).TRACKING_CONCURRENCY + TRACKING_PRIORITY_SLOTS.server/src/lib/first-run-rollups.js): only on a brand's first full run, the rollups refresh as answers arrive, one at a time, at most everymax(2 min, 5 × the last refresh).run-settlement.js).Not done (cost, limits or data risk):
TRACKING_CONCURRENCYoverall (memory);Related issue
None.
Stacked on #1772 (first run from the webhook too). The diff against it holds only this change.
Type of change
perf— PerformanceHow to test
cd server && npx vitest run src/lib/job-runner.test.js src/lib/first-run-rollups.test.js src/lib/run-settlement.test.js src/workers/tracking-worker.first-run.test.js. These cover:Checklist
🤖 Generated with Claude Code