feat(api): implement per-user edit token for settings mutations

This commit is contained in:
shay
2026-04-19 09:34:26 -04:00
parent 3ecb6ad67b
commit 016583c9f3
16 changed files with 774 additions and 116 deletions
+22 -1
View File
@@ -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():