chore(api): add asynchronous build kickoff for improved responsiveness

This commit is contained in:
shay
2026-04-18 11:26:25 -04:00
parent 3ffb2cfb6f
commit f9c59d934d
2 changed files with 35 additions and 2 deletions
+26
View File
@@ -5,12 +5,33 @@ Serves:
/assets/<p> -> SPA bundles
/api/* -> JSON/SVG API
"""
import logging
import os
from functools import wraps
from threading import Thread
from flask import Flask, jsonify, request, Response, send_from_directory
from . import config, db, fetcher_client, placeholder
log = logging.getLogger("generator.api")
def _kickoff_build():
"""Run one build cycle in a background thread. Eliminates the worker's
poll latency for users who just enrolled or patched settings. The worker
container still drains the queue; this just gives interactive requests
a head start so the build runs concurrently with the user filling out
the workshop page."""
try:
from . import worker
worker.process_one()
except Exception:
log.exception("background build kickoff failed")
def _kickoff_build_async():
Thread(target=_kickoff_build, daemon=True).start()
_STATIC_DIR = os.path.join(os.path.dirname(__file__), "static")
app = Flask(__name__, static_folder=None)
@@ -73,6 +94,7 @@ def get_user_data(username: str):
"widget_order": config.WIDGET_ORDER,
}
db.enroll(username, defaults)
_kickoff_build_async()
return jsonify({"status": "building"}), 202
db.touch_last_requested(username)
@@ -101,6 +123,7 @@ def _serve(username: str, widget_name: str) -> Response:
"widget_order": config.WIDGET_ORDER,
}
db.enroll(username, defaults)
_kickoff_build_async()
return _placeholder_response("building", username)
db.touch_last_requested(username)
@@ -135,6 +158,7 @@ def enroll_endpoint():
return jsonify({"error": "rate_limited"}), 429
defaults = {"theme": "dark", "enabled": config.ENABLED_WIDGETS, "widget_order": config.WIDGET_ORDER}
job_id = db.enroll(username, defaults)
_kickoff_build_async()
return jsonify({"enrolled": True, "job_id": job_id})
@@ -156,6 +180,7 @@ def patch_settings(username: str):
body = request.get_json(silent=True) or {}
merged = {**current["settings"], **body}
job_id = db.update_settings(username, merged)
_kickoff_build_async()
return jsonify({"updated": True, "job_id": job_id})
@@ -172,6 +197,7 @@ def refresh(username: str):
except Exception as e:
return jsonify({"error": f"fetch failed: {e}"}), 502
job_id = db.enqueue_build(username)
_kickoff_build_async()
return jsonify({"refreshed": True, "job_id": job_id})
@@ -1,5 +1,5 @@
"""End-to-end: api + worker in-process + mocked fetcher HTTP."""
import os, tempfile
import os, tempfile, time
import pytest
import responses
from src import api as apimod, db as dbmod, worker
@@ -30,10 +30,17 @@ def test_first_request_builds_then_serves_ready(env):
r1 = client.get("/api/alice")
assert r1.headers["X-Widget-Status"] == "building"
# First request kicks off a background build thread; drain any residual
# job in case of races, then poll the API for readiness.
for _ in range(5):
if not worker.process_one():
break
deadline = time.time() + 2.0
while time.time() < deadline:
r2 = client.get("/api/alice")
if r2.headers["X-Widget-Status"] == "ready":
break
time.sleep(0.05)
assert r2.headers["X-Widget-Status"] == "ready"
assert r2.data.startswith(b"<svg") or r2.data.startswith(b"<?xml")