Skip to content

fix(tracking): stop waiting on ghost Cloro tasks and de-synchronise pulses - #690

Merged
gkhngyk merged 1 commit into
mainfrom
fix/687-ghost-task-waits
Aug 10, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/687-ghost-task-waits

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Cloro accepts tasks it never calls back on — routine for google-aio, which has nothing to return when a query has no AI Overview. Both drain loops counted those forever-pending rows, so every run burned its full stall window and every brand parked for the full 90-minute pulse cap.

That cost more than time. Because the wait was a fixed duration, brands whose runs finished minutes apart woke in the same second, each firing a dozen aggregate queries in parallel. On 2026-08-10 the largest brand's statements crossed PostgREST's 8s statement_timeout and its digest was discarded entirely:

"msg":"daily pulse generation failed"
"message":"ai_visibility_aggregates failed: canceling statement due to statement timeout"

Measured with the database otherwise idle, that same RPC takes 526 ms — it is not slow on its own, it becomes slow under the concurrency the wait manufactures. (Buffers: shared hit=126238, spilling to disk at work_mem=3500kB; that brand holds 41k of the table's 132k rows.)

Observed wake-up waves:

when brands pulsed in the same second
08-09 07:36 6 (incl. the 1791-result brand — passed, barely)
08-10 06:46 4 (the 1791-result brand failed)

What changed

  • Worker drain: exits when delivery has gone quiet and every remaining task is older than 30 minutes. Both conditions are required — a quiet gap alone is normal mid-burst (a real run went silent for 10+ minutes then delivered ~1000 more results), and old tasks alone are fine while results are still landing. The MIN_STAMP_RATIO partial-run guard is untouched, so a genuinely half-delivered run still refuses to stamp.
  • Pulse drain: ignores pending tasks older than 45 minutes, and no longer treats a failed poll as an empty queue.
  • Pulse metrics run sequentially. Nothing waits on this job interactively, so the parallelism bought no perceived speed and only multiplied one brand's peak database load.
  • Transient DB errors retry twice with backoff instead of discarding the digest.
  • TRACKING_CONCURRENCY env (default 2) replaces the hardcoded ceiling inherited from the old Bull setup.

Expected effect: per-brand wait drops from ~60 min to ~15 min, the nightly cycle shortens accordingly, and pulses land minutes after each run's stamp instead of on a synchronised 90-minute delay.

Related issue

Closes #687

Type of change

  • fix — Bug fix

How to test

cd server && yarn test

12 new tests cover the parts worth pinning:

  • allTasksAreStale — true only when every pending task is past the cutoff; false for a mixed batch (the dangerous case: exiting there would abandon a task that is merely mid-burst), false for an empty list, false at the exact cutoff, and false when a row has no submitted_at (unknown age must not be guessed as old).
  • series — never overlaps (asserted via a concurrency counter), preserves input order like Promise.all, and propagates failures without starting the remaining work.
  • isTransientDbError — matches the production timeout string and deadlocks, and does not retry genuine errors like a missing column or a permission failure.

In production, after deploy the log line to watch is cloro tasks are ghosts — delivery quiet and every remaining task is stale; continuing, and pulse created_at values should sit minutes after each run's completed_at rather than clustering.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • Commits follow Conventional Commits
  • yarn lint passes — server ESLint clean with --max-warnings=0
  • yarn typecheck passes — no web sources touched
  • yarn format:check passes
  • Changes are focused — one concern per PR

Deployment note

No env changes are required; the defaults reproduce today's behaviour minus the ghost waits. CLORO_GHOST_STALL_POLLS (default 60 ≈ 15 min) and TRACKING_CONCURRENCY (default 2) are documented in server/.env.example if you want to tune the cycle further after watching one night.

…ulses

Cloro accepts tasks it never calls back on — routine for google-aio, which
has nothing to return when a query has no AI Overview. Both the worker's
drain loop and the pulse's drain wait counted those forever-pending rows,
so every run burned its full stall window and every brand parked for the
full 90-minute pulse cap.

That cost more than time. Because the wait was a fixed duration, brands
whose runs finished minutes apart woke in the same second and each fired a
dozen aggregate queries in parallel; the largest brand's statements crossed
PostgREST's 8s statement_timeout and its digest was discarded entirely.

- worker: exit the drain when delivery has gone quiet AND every remaining
  task is older than 30 minutes. Both conditions are required — a quiet gap
  alone is normal mid-burst, and old tasks alone are fine while results are
  still landing. The partial-run stamp guard is untouched.
- pulse: ignore pending tasks older than 45 minutes when waiting for the
  queue to drain, and don't read a failed poll as an empty queue.
- pulse: run the metric queries sequentially. Nothing waits on this job
  interactively, so concurrency only multiplied peak database load.
- pulse: retry a statement timeout twice before giving up, instead of
  throwing the whole digest away.
- concurrency is now configurable via TRACKING_CONCURRENCY (default 2).

Closes #687
@gkhngyk
gkhngyk merged commit 0608c01 into main Aug 10, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/687-ghost-task-waits branch August 10, 2026 11:04
@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.

Tracking cycle: stop waiting on ghost Cloro tasks — cuts the nightly cycle from ~5h to ~1.5h

1 participant