mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(api): add idempotency check for already queued builds in callback
This commit is contained in:
+23
-8
@@ -528,20 +528,30 @@ def auth_github_callback():
|
||||
session["gh_id"] = gh_id
|
||||
session["gh_avatar_url"] = avatar_url
|
||||
|
||||
# Implicit enrollment. Idempotent; refreshes github_avatar_url on return
|
||||
# logins. We deliberately do NOT enqueue a build job here — the build is
|
||||
# added to the queue by /internal/data-ready when the fetcher finishes
|
||||
# the GitHub fetch. Enqueuing earlier means the worker pops a job before
|
||||
# the fetcher has data, blocking on a synchronous GitHub fetch that can
|
||||
# easily exceed the worker's HTTP timeout.
|
||||
# Implicit enrollment. Three converging paths kick off the build, all
|
||||
# idempotent — the goal is that no single failure (fetcher unreachable,
|
||||
# worker process not running, callback mis-delivered) can strand the
|
||||
# signup waiting on cron's 15-min tick.
|
||||
# 1) db.enroll() enqueues a 'build' job. Worker container or the
|
||||
# in-process kickoff pool will pop it; if the fetcher has no
|
||||
# data yet, /data auto-fetches synchronously (fast now that the
|
||||
# pipeline is parallelized end-to-end).
|
||||
# 2) _request_fetch_async asks the fetcher to start a background
|
||||
# fetch immediately. When it finishes, it calls back to
|
||||
# /internal/data-ready, which is a no-op if a pending build for
|
||||
# this user already exists — it just shortens latency in the
|
||||
# common case.
|
||||
# 3) _kickoff_prefetch_async runs process_one() in this process's
|
||||
# thread pool. This is the safety net for deployments that don't
|
||||
# run a separate worker process (gunicorn-only setups).
|
||||
defaults = {
|
||||
"theme": "dark",
|
||||
"enabled": config.ENABLED_WIDGETS,
|
||||
"widget_order": config.WIDGET_ORDER,
|
||||
}
|
||||
db.enroll(login, defaults, github_id=gh_id, github_avatar_url=avatar_url,
|
||||
enqueue_build=False)
|
||||
db.enroll(login, defaults, github_id=gh_id, github_avatar_url=avatar_url)
|
||||
_request_fetch_async(login)
|
||||
_kickoff_prefetch_async(login)
|
||||
|
||||
return redirect(nxt)
|
||||
|
||||
@@ -580,6 +590,11 @@ def internal_data_ready():
|
||||
# build that would just fail too. The cron loop will retry the
|
||||
# fetch on its next tick.
|
||||
return jsonify({"ignored": True, "reason": "fetch_failed"}), 200
|
||||
# Idempotent: the OAuth callback already enqueues a build directly so
|
||||
# the worker can make progress even if this callback never arrives.
|
||||
# If a build is already pending/running, don't queue a second one.
|
||||
if db.has_open_build(username):
|
||||
return jsonify({"ignored": True, "reason": "already_queued"}), 200
|
||||
job_id = db.enqueue_build(username)
|
||||
log.info("enqueued build %d for %s after fetch completion", job_id, username)
|
||||
return jsonify({"queued": True, "job_id": job_id}), 200
|
||||
|
||||
@@ -357,6 +357,19 @@ def enqueue_build(username: str) -> int:
|
||||
return cur.lastrowid
|
||||
|
||||
|
||||
def has_open_build(username: str) -> bool:
|
||||
"""True if the user has a pending or running build job. Used by callers
|
||||
that want enqueue idempotency without taking on the SQL themselves."""
|
||||
with _settings_conn() as c:
|
||||
row = c.execute(
|
||||
"""SELECT 1 FROM jobs
|
||||
WHERE username=? AND kind='build' AND status IN ('pending', 'running')
|
||||
LIMIT 1""",
|
||||
(username,),
|
||||
).fetchone()
|
||||
return row is not None
|
||||
|
||||
|
||||
def enqueue_build_all() -> int:
|
||||
"""Enqueue a 'build' job for every enrolled user that doesn't already
|
||||
have one pending. Returns the number inserted. Idempotent — safe to
|
||||
|
||||
@@ -355,6 +355,25 @@ def test_data_ready_does_not_enqueue_on_failed_fetch(client, monkeypatch):
|
||||
assert dbmod.pending_job_count() == 0
|
||||
|
||||
|
||||
def test_data_ready_is_idempotent_when_build_already_pending(client, monkeypatch):
|
||||
"""The OAuth callback now enqueues a build directly so the worker can
|
||||
make progress even if /internal/data-ready never arrives. When the
|
||||
callback DOES arrive after that, it must not queue a second build."""
|
||||
monkeypatch.setattr(cfg, "FETCHER_INTERNAL_TOKEN", "secret")
|
||||
dbmod.enroll("alice", {"theme": "dark"}) # default enqueue_build=True
|
||||
assert dbmod.pending_job_count() == 1
|
||||
|
||||
r = client.post("/internal/data-ready",
|
||||
headers={"X-Internal-Token": "secret"},
|
||||
json={"username": "alice", "payload_hash": "h1", "ok": True})
|
||||
assert r.status_code == 200
|
||||
assert r.get_json()["ignored"] is True
|
||||
assert r.get_json()["reason"] == "already_queued"
|
||||
# Critically, no second build job — the worker would just process the
|
||||
# same widgets twice with no benefit.
|
||||
assert dbmod.pending_job_count() == 1
|
||||
|
||||
|
||||
def test_query_override_triggers_adhoc_render_without_persisting(client):
|
||||
"""GET /api/<u>?theme=... renders on-demand via worker.render_composite_adhoc
|
||||
and does not mutate the stored settings blob — this is the path visitors
|
||||
|
||||
Reference in New Issue
Block a user