Warmers build pages as each admin account; log slow page builds

The background warmers fetched pages as an anonymous admin, while a logged-in
admin's pages are cached under their own account, so no warmed page was ever
served: every visit after a change waited for its page to be built. The
warmers now build the pages as each admin account (auth.warm_viewers, passed
as a WSGI key a request can't set). Page builds slower than 1.5 s are logged,
marked visit or warm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-10-26 14:24:00 +02:00
parent 105698082b
commit c54f66c3c2
3 changed files with 28 additions and 9 deletions

View file

@ -120,8 +120,10 @@ def _user_profile(r):
def from_request(): def from_request():
"""The viewer of this request (web.py's before_request puts it in g.viewer).""" """The viewer of this request (web.py's before_request puts it in g.viewer)."""
if not active() or request.environ.get("dash.internal"): # no accounts yet / the warmers if not active(): # no accounts yet
return ADMIN return ADMIN
if request.environ.get("dash.internal"): # the warmers: as an admin account (warm_viewers)
return request.environ.get("dash.viewer") or ADMIN
tok = request.cookies.get(COOKIE) tok = request.cookies.get(COOKIE)
if tok: if tok:
h = hashlib.sha256(tok.encode()).hexdigest() h = hashlib.sha256(tok.encode()).hexdigest()
@ -137,6 +139,15 @@ def from_request():
return household() return household()
def warm_viewers():
"""Whom the page warmers build pages for: each admin account, so a warmed page is the very one
(cache_key, "Logged in as …") an admin gets; no accounts yet: the anonymous admin."""
if not active():
return [ADMIN]
with closing(_db()) as con:
return [_user_profile(r) for r in con.execute("SELECT * FROM user WHERE role = 'admin' ORDER BY id")] or [ADMIN]
_owner = threading.local() _owner = threading.local()

View file

@ -129,7 +129,12 @@ class Context:
if html is not None: if html is not None:
pages.move_to_end(key) pages.move_to_end(key)
return html return html
t0 = time.monotonic()
html = fn(*a, **kw) # outside the lock: other pages stay served html = fn(*a, **kw) # outside the lock: other pages stay served
took = time.monotonic() - t0
if took > SLOW_PAGE: # what a visit waits for on the Pi (journalctl -u dashboard)
who = "warm" if request.environ.get("dash.internal") else "visit"
print(f"plugin {self.name}: {request.full_path.rstrip('?')} built in {took:.1f} s ({who})", flush=True)
if isinstance(html, str): if isinstance(html, str):
with _cache_lock: with _cache_lock:
pages[key] = html pages[key] = html
@ -291,6 +296,7 @@ class Context:
CACHE_PAGES = 40 CACHE_PAGES = 40
SLOW_PAGE = 1.5 # s: cached pages built slower than this are logged
_cache_lock = threading.Lock() _cache_lock = threading.Lock()
WARM_EVERY = 60 WARM_EVERY = 60
_code = {"t": 0, "v": None} _code = {"t": 0, "v": None}
@ -328,8 +334,9 @@ def _warmer(app, store):
if p.error or not enabled(store, name): if p.error or not enabled(store, name):
continue continue
for path in p.warm: for path in p.warm:
for v in auth.warm_viewers(): # as each admin account: the pages they get (auth.cache_key)
try: try:
client.get(path, environ_base={"dash.internal": True}) # as admin (auth) client.get(path, environ_base={"dash.internal": True, "dash.viewer": v})
except Exception as e: except Exception as e:
print(f"plugin {name}: warm {path}: {type(e).__name__}: {e}", flush=True) print(f"plugin {name}: warm {path}: {type(e).__name__}: {e}", flush=True)
time.sleep(WARM_EVERY) time.sleep(WARM_EVERY)

View file

@ -452,8 +452,9 @@ def _warm(app):
time.sleep(15) # the workers' first fetches come first time.sleep(15) # the workers' first fetches come first
client = app.test_client() client = app.test_client()
for path in WARM: for path in WARM:
for v in auth.warm_viewers(): # as each admin account (auth.from_request)
try: try:
client.get(path, environ_base={"dash.internal": True}) # as admin (auth) client.get(path, environ_base={"dash.internal": True, "dash.viewer": v})
except Exception as e: except Exception as e:
print(f"web: warm {path}: {type(e).__name__}: {e}", flush=True) print(f"web: warm {path}: {type(e).__name__}: {e}", flush=True)