fix(fetcher): keep last-good counts on GitHub rate limits + expose fetch metrics

Rate-limited / 5xx / network failures were silently returning 0 from the
commit- and PR-count paths, then overwriting good values in fetcher.db —
turning a traffic spike into permanently-wrong "0 commits" cards. Raise
GitHubTransientError instead so the fetch aborts and the last-good row is
retained; genuine NOT_FOUND still yields 0.

Add a shared fetch-outcome counter (fetch_metrics table, spanning the web
workers and the cron container) surfaced at an unauthenticated GET /metrics
as ghstats_fetch_total{outcome="ok|not_found|rate_limited|error"}, so rate
limits are visible to the monitoring stack instead of dying silently.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
shay
2026-07-18 11:45:42 -07:00
co-authored by Claude Opus 4.8
parent d1e89fb1c3
commit 0586f83a33
8 changed files with 227 additions and 4 deletions
+17
View File
@@ -4,6 +4,7 @@ import pytest
from unittest.mock import patch
from src import db as dbmod
from src import cron as cronmod
from src import github as ghmod
@pytest.fixture
@@ -26,6 +27,22 @@ def test_refresh_tick_calls_github_for_due_users(tmp_db):
assert stats["refreshed"] == 1
def test_refresh_rate_limit_keeps_last_good_and_counts_metric(tmp_db):
# A rate-limited refresh must NOT overwrite the cached row, and must be
# counted as rate_limited so the monitoring stack can see it.
dbmod.upsert_user("alice", {"user": {"login": "alice"}, "total_commits": 1841})
with dbmod._connect() as c:
c.execute("UPDATE users SET fetched_at='2020-01-01T00:00:00Z', last_requested_at=? WHERE username='alice'",
(dbmod._now(),))
with patch("src.cron.github.fetch_github_data",
side_effect=ghmod.GitHubTransientError("rate limited")):
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
assert stats["failed"] == 1 and stats["refreshed"] == 0
# Last-good data retained.
assert dbmod.get_user("alice")["data"]["total_commits"] == 1841
assert dbmod.read_fetch_metrics()["rate_limited"] == 1
def test_tick_runs_gc_for_abandoned_trial_users(tmp_db):
dbmod.upsert_user("ghost", {"user": {"login": "ghost"}})
with dbmod._connect() as c: