mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(api): implement per-user edit token for settings mutations
This commit is contained in:
+22
-1
@@ -3,18 +3,37 @@
|
||||
Run as its own container: CMD python -m src.cron
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
|
||||
from . import config, db, fetcher_client
|
||||
|
||||
log = logging.getLogger("generator.cron")
|
||||
|
||||
# Pacing between users in a single tick. On a mini PC with N enrolled
|
||||
# users, a tight loop would fire N fetcher calls back-to-back, each of
|
||||
# which can become a GitHub call — trivially enough to spike CPU and
|
||||
# burn PAT budget. Default 0.25s keeps 1000 users inside a ~4-minute
|
||||
# tick while leaving headroom in the 15-minute window.
|
||||
_TICK_INTER_USER_SLEEP_S = float(os.getenv("GENERATOR_CRON_INTER_USER_SLEEP_S", "0.25"))
|
||||
|
||||
# Queue-depth backstop. If the build worker is already behind, don't
|
||||
# enqueue more: let it drain. Keeps SQLite from swelling when a burst of
|
||||
# GitHub changes arrives at once.
|
||||
_TICK_QUEUE_CAP = int(os.getenv("GENERATOR_CRON_QUEUE_CAP", str(config.PENDING_JOB_QUEUE_CAP)))
|
||||
|
||||
|
||||
def tick() -> dict:
|
||||
enqueued = 0
|
||||
failed = 0
|
||||
skipped_queue_full = 0
|
||||
for username in db.list_enrolled():
|
||||
try:
|
||||
if db.pending_job_count() >= _TICK_QUEUE_CAP:
|
||||
skipped_queue_full += 1
|
||||
# Don't hammer the fetcher either when the queue is full.
|
||||
time.sleep(_TICK_INTER_USER_SLEEP_S)
|
||||
continue
|
||||
info = db.get_settings(username)
|
||||
current = info.get("last_fetcher_payload_hash")
|
||||
r = fetcher_client.get_data(username)
|
||||
@@ -26,7 +45,9 @@ def tick() -> dict:
|
||||
except Exception as e:
|
||||
log.warning("poll failed for %s: %s", username, e)
|
||||
failed += 1
|
||||
return {"enqueued": enqueued, "failed": failed}
|
||||
if _TICK_INTER_USER_SLEEP_S > 0:
|
||||
time.sleep(_TICK_INTER_USER_SLEEP_S)
|
||||
return {"enqueued": enqueued, "failed": failed, "skipped_queue_full": skipped_queue_full}
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user