Skip to content

perf(tracking): start the first run after a payment at once, and fill its pages as answers arrive - #1773

Merged
gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/first-run-speed
Oct 29, 2026
Merged

gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/first-run-speed

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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:

  • A first run waited for a free slot with the nightly runs, which hold slots for 5–90+ minutes, so it could wait minutes to hours.
  • Citations, Mentions and Visibility's 7/30/90-day views stayed empty until the run was stamped, and the late-stamp check ran every 2 minutes.
  • Cloro's own queue (a first answer in roughly 4–65 minutes) is outside our control.

Changes

  • Queue priority: the run started after a payment (afterPayment) may use one extra slot (TRACKING_PRIORITY_SLOTS, default 1, documented in server/.env.example).
    • It checks for a slot every second, and other jobs step aside while it waits.
    • Total concurrency stays capped at TRACKING_CONCURRENCY + TRACKING_PRIORITY_SLOTS.
    • Recovery and retries keep the priority.
  • Progressive rollups (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 every max(2 min, 5 × the last refresh).
    • Before stamping, the run waits for a refresh in flight (run-settlement.js).
    • During a late wait, each late poll refreshes too, so late answers show.
    • The SQL refreshes already take a per-brand advisory lock; serializing them in JS keeps waiting connections from piling up.
  • Late-stamp check: every 30 s instead of 2 min on a first run, so the Action Center fills up to ~1.5 min sooner.
  • Later runs behave exactly as before.

Not done (cost, limits or data risk):

  • a priority queue or higher concurrency at Cloro (likely paid);
  • raising TRACKING_CONCURRENCY overall (memory);
  • counting silent Cloro tasks as done early (late answers would fall outside the 24 h window).

Related issue

None.

Stacked on #1772 (first run from the webhook too). The diff against it holds only this change.

Type of change

  • perf — Performance

How 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:

  • the priority order (a mutation check confirms the test catches its removal);
  • priority kept on retry;
  • paced refreshes;
  • the stamp waiting for a running refresh;
  • nothing changing on later runs.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Server: format, lint, 283 test files / 3,948 tests (3 skipped) pass
  • Web: lint (0 errors, 7 warnings), typecheck, 556 test files / 5,339 tests and build pass
  • Reviewed before opening; the review's findings are in this branch

🤖 Generated with Claude Code

cihan-gyk and others added 4 commits October 29, 2026 14:24
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]>
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 gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-tracking created gets priority, and recovery keeps it by data.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 at TRACKING_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_runs reads id; listWaitingJobs adds data).
  • Migrations: none. Read-only gate: no web change in this part.

Follow-ups (not blocking)

  • isFirstRun means "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.

@gkhngyk
gkhngyk merged commit 5f5453b into ansvisor:main Oct 29, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants