mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
fix(fetcher): quota reserve and per-tick cap for refresh cron
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+26
-2
@@ -2,17 +2,41 @@
|
||||
import logging
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from . import config, db, github
|
||||
|
||||
log = logging.getLogger("fetcher.cron")
|
||||
|
||||
|
||||
def _core_remaining() -> int | None:
|
||||
"""Core REST calls left on the PAT, or None if unknown. GitHub does not
|
||||
count /rate_limit calls against the quota."""
|
||||
if not config.GITHUB_PAT:
|
||||
return None
|
||||
try:
|
||||
r = requests.get("https://api.github.com/rate_limit",
|
||||
headers={"Authorization": f"Bearer {config.GITHUB_PAT}"},
|
||||
timeout=config.API_TIMEOUT)
|
||||
return int(r.json()["resources"]["core"]["remaining"])
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def tick(hours: int, active_within_days: int, gc_days: int) -> dict:
|
||||
"""Refresh due users, then GC abandoned ones. Returns counts."""
|
||||
refreshed = 0
|
||||
failed = 0
|
||||
due = db.users_due_for_refresh(hours=hours, active_within_days=active_within_days)
|
||||
for username in due:
|
||||
deferred = max(len(due) - config.REFRESH_MAX_PER_TICK, 0)
|
||||
due = due[:config.REFRESH_MAX_PER_TICK]
|
||||
for i, username in enumerate(due):
|
||||
left = _core_remaining()
|
||||
if left is not None and left < config.REFRESH_CORE_RESERVE:
|
||||
log.info("core budget %s under reserve %s; deferring %s refreshes",
|
||||
left, config.REFRESH_CORE_RESERVE, len(due) - i)
|
||||
deferred += len(due) - i
|
||||
break
|
||||
try:
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
user = data.get("user")
|
||||
@@ -32,7 +56,7 @@ def tick(hours: int, active_within_days: int, gc_days: int) -> dict:
|
||||
log.warning("refresh failed for %s: %s", username, e)
|
||||
failed += 1
|
||||
gc_removed = db.delete_stale(days=gc_days)
|
||||
return {"refreshed": refreshed, "failed": failed, "gc_removed": gc_removed}
|
||||
return {"refreshed": refreshed, "failed": failed, "deferred": deferred, "gc_removed": gc_removed}
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user