fix: Spotify 429 backoff + workflow-native chunk retries (memoized actor fetch, durable mapping)

Merged
SW
6mo ago
+243-70

What

The 570-album re-snapshot (chat#1794 item 2) exposed the gap: all 6 actor chunks succeeded (~$1.71 spent, 570 albums scraped), but only 2 chunks' identifier mappings landed (420 of ~4,000 expected measurements). Root cause: ~85 Spotify /v1/tracks batch lookups in 2.5 minutes → 429s → getTracks had no retry → mapUnmappedAlbumTracks degraded to mapped-only capture for 4 chunks (by design, silently).

Fix: per-batch Retry-After backoff in getTracks, up to 3 retries; non-429 failures still fail fast. Tests: retry-then-succeed, exhaust-then-error, non-429 no-retry (RED→GREEN).

Recovery plan

After merge: one re-snapshot heals everything — already-mapped tracks skip Spotify entirely (no lookups), so the retry only has to cover the remaining ~3,500 resolutions, now with backoff. Verification = identifier/measurement coverage counts on the PR.

Diagnostic trail on chat#1794. Apify-side spend is not wasted — the scrape data was fine; only the mapping degraded.

🤖 Generated with Claude Code


Summary by cubic

Adds 429 backoff to Spotify getTracks and splits snapshot capture into fetch and map+write steps with durable retries and memoized actor results. Also dedupes songs by ISRC before upsert to keep multi-album chunks from failing and prevent partial captures.

  • Bug Fixes

    • Per-batch 429 backoff using Retry-After, up to 3 retries; throws SpotifyRateLimitError on exhaustion; non-429 errors fail fast.
    • Deduplicate songs by ISRC across albums before upsertSongs to avoid in-batch ON CONFLICT updates on the same row; maintains bootstrap coverage for reissues.
    • Tests cover retry success, retry exhaustion, non-429 handling, and ISRC dedupe.
  • Refactors

    • Split chunk capture into fetchChunkStep (actor call, result memoized) and mapAndWriteChunkStep (mapping + write).
    • Escalate sustained Spotify rate limiting to a workflow RetryableError (retryAfter 30s); mapUnmappedAlbumTracks rethrows SpotifyRateLimitError.

Summary by CodeRabbit

  • New Features

    • Added automatic retry logic for Spotify API rate limits with intelligent backoff using the service's Retry-After header.
  • Refactor

    • Restructured snapshot capture workflow into modular steps for improved durability and error handling.
    • Enhanced rate limit error escalation with durable retry behavior across workflow execution.