Skip to content

fix(tracking): stop discarding runs whose first result lands late - #710

Merged
gkhngyk merged 1 commit into
mainfrom
fix/702-drain-deadline
Aug 12, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/702-drain-deadline

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

The drain loop had a single 60-minute budget measured from submission. On the largest brand, Cloro's first callback landed 62–65 minutes after submission three nights running, so the worker gave up two to five minutes before the delivery it was waiting for.

The consequences compounded: with zero results at stamp time the ledger row was deleted, the 24h anchor never moved, and both the dashboard and the pulse mail stayed frozen on the previous day while ~1800 results landed minutes later. The same figures went out by email three times.

08-08 08-09 08-10 08-11 08-12
Stamped run 1674 1789 1791 none none
Results actually stored 1668 1783 1946 1778 1381
Figure the pulse reported 2246 1789 — 1785 ×2 1785

Job durations confirm the cap was what fired, not the stall exit: 60.5 and 61.3 minutes on the two failing nights, with the first result arriving at 62 and 65 minutes. The brand tracks only scraper platforms, so there is no API-model phase to account for the time.

The fix splits the budget in two. Waiting for the first result and waiting out the tail are different situations, and only the second has anything to measure — the stall and ghost exits both compare successive pending counts, so they are meaningless before anything has come back. That phase gets its own allowance (CLORO_FIRST_RESULT_WAIT_MIN, default 90), and the tail is measured from the first result rather than from submission (CLORO_DRAIN_TAIL_MIN, default 60), so a slow queue start no longer eats the time the rest of the delivery needs. Stall and ghost detection now only run once delivery has started.

Two things came with it:

  • The drain always logs how it ended — reason, elapsed time, and how long the first result took. The timed-out path used to fall out of the loop silently, which is why this took three mornings of database forensics rather than one log line.
  • The Daily Pulse is skipped when a run wasn't stamped. Such a run left the anchor on the previous run, so its pulse recomputes that same window and mails figures the recipient already has — exactly what happened here. The catch-up sweep picks the day back up once a run does stamp. This also removes the main way bug: duplicate Daily Pulse emails when the catch-up sweep and the run's own pulse share a window #701 shows up in practice, though the anchor-based dedupe in that issue is still worth doing on its own.

The time budget decision is extracted as a pure function so it is testable without a live drain.

Related issue

Closes #702

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Behaviour is timing-dependent, so the budget logic is covered by unit tests (yarn test from server/, 179 passing, 5 new). What to check in production after the next nightly cycle:

  1. The largest brand gets a tracking_runs row again, with result_count matching the day's results.
  2. cloro delivery started appears in the logs with waitedMs — that number is the thing this PR is about.
  3. cloro drain finished (drained) rather than (no_first_result).
  4. The pulse mail reports the current day's figures rather than repeating the previous day's.
  5. A brand whose run genuinely produces nothing still has its ledger row removed, and now logs the skipped pulse rather than sending a stale one.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

The drain loop had one 60-minute budget measured from submission. On the
largest brand Cloro's first callback landed 62-65 minutes after submission
three nights running, so the worker gave up two to five minutes before the
delivery it was waiting for. With zero results at stamp time the ledger row
was deleted, and because the 24h anchor never moved, the dashboard and the
pulse mail both stayed frozen on the previous day while ~1800 results landed
minutes later. The same figures went out by email three times.

Split the budget in two. Waiting for the first result and waiting out the
tail are different situations, and only the second has anything to measure:
the stall and ghost exits both compare successive pending counts, so they
are meaningless before anything has come back. That phase now has its own
allowance (CLORO_FIRST_RESULT_WAIT_MIN, default 90), and the tail is
measured from the first result rather than from submission
(CLORO_DRAIN_TAIL_MIN, default 60), so a slow queue start no longer eats
the time the rest of the delivery needs. Stall and ghost detection only run
once delivery has started.

The drain now always logs how it ended — reason, elapsed time, and how long
the first result took. The timed-out path used to fall out of the loop
silently, which is why this took three mornings of database forensics to
pin down.

Also skip the Daily Pulse when a run wasn't stamped: such a run left the
anchor on the previous run, so its pulse recomputes that window and mails
figures the recipient already has. The catch-up sweep picks the day back up
once a run does stamp.

Closes #702
@gkhngyk gkhngyk self-assigned this Aug 12, 2026
@gkhngyk
gkhngyk merged commit e502569 into main Aug 12, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/702-drain-deadline branch August 12, 2026 17:30
@gkhngyk gkhngyk mentioned this pull request Aug 24, 2026
7 of 12 tasks
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.

bug: tracking run is discarded when the scraper's first delivery lands after the 60-minute drain deadline

1 participant