fix: security price sync reliability (TwelveData errors, offline resolver, health checker, missing names)

Merged
GF
1mo ago
+1128-95

Fixes #3097

Summary

  • Provider::TwelveData#default_error_transformer now parses the real API error body instead of surfacing Faraday's generic HTTP status message, so the existing plan-restriction detection (Security.plan_upgrade_required?) actually works and real messages like "available starting with the Grow or Venture plan" reach the debug log/UI instead of a meaningless "status 404".
  • Security::Resolver distinguishes a technical provider-search failure (rate limit/timeout) from a genuine "no match" — technical failures get offline_reason: "resolution_pending" instead of the generic offline fallback, so a future retry can pick them up.
  • Security::Resolver#reactivate_if_provider_available! now also reactivates when the caller explicitly passed a price_provider (i.e. the user picked this exact security+provider from search results), not only when offline_reason == "provider_disabled" — matching the trust level HoldingsController#remap_security already applies unconditionally.
  • Security::HealthChecker only deletes a security's price history on an offline transition if it never had a successfully-fetched price; existing history is preserved. It also no longer counts "no provider available" as a fetch failure, so it can't silently overwrite a more specific offline_reason (e.g. "provider_disabled") over time.
  • Security#find_or_fetch_price now logs failed provider fetches to the debug event log (deduped to at most once per security per day).
  • Provider::YahooFinance#fetch_chart_data logs its previously-swallowed FX fetch errors; its Faraday retry middleware now also retries transient 5xx statuses, not only 429.
  • Security::Resolver#find_or_create_provider_match! now backfills name directly from the search result (only when blank, never overwriting an existing name) instead of relying entirely on a separate fetch_security_info//profile call, which appears to be plan-gated for effectively all symbols on TwelveData.
  • Daily market data import time is now configurable via /settings/hosting (mirrors the existing auto_sync_time/AutoSyncScheduler pattern), replacing the hardcoded 22:00 UTC entry in config/schedule.yml. Default behavior is unchanged unless actively reconfigured.

See #3097 for the full writeup of each bug with root cause.

Test plan

  • bin/rails test — full suite, no regressions (pre-existing environmental failures only: Snaptrade/Stripe/AI config gaps unrelated to this change)
  • New/updated unit tests for every change (resolver, health checker, provided, twelve_data, yahoo_finance, market_data_scheduler, hostings_controller)
  • bin/rubocop — clean
  • bin/brakeman — 0 warnings
  • Live-verified in a real self-hosted instance against the live TwelveData API:
    • Confirmed the real plan-restriction message now surfaces for a restricted symbol
    • Confirmed a security stuck offline reactivates when re-selected with a valid provider
    • Confirmed price history survives an offline transition
    • Confirmed the configurable import cron job fires at the configured time and completes
    • Confirmed a security name now populates immediately from search results

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added settings to enable daily market-data synchronization and choose its run time and timezone.
    • Synchronization updates automatically when settings change and follows local weekday schedules.
    • Added validation and safe defaults for invalid schedule times.
  • Bug Fixes

    • Improved provider error messages and retries for temporary service failures.
    • Preserved historical prices during outages and improved security recovery behavior.
    • Better distinguishes temporary provider failures from genuine no-match results.
    • Added clearer warnings, structured diagnostics, and reduced duplicate failure notifications.
    • Disabling synchronization now removes its scheduled job.