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:
parent
105698082b
commit
c54f66c3c2
3 changed files with 28 additions and 9 deletions
|
|
@ -120,8 +120,10 @@ def _user_profile(r):
|
|||
|
||||
def from_request():
|
||||
"""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
|
||||
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)
|
||||
if tok:
|
||||
h = hashlib.sha256(tok.encode()).hexdigest()
|
||||
|
|
@ -137,6 +139,15 @@ def from_request():
|
|||
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()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -129,7 +129,12 @@ class Context:
|
|||
if html is not None:
|
||||
pages.move_to_end(key)
|
||||
return html
|
||||
t0 = time.monotonic()
|
||||
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):
|
||||
with _cache_lock:
|
||||
pages[key] = html
|
||||
|
|
@ -291,6 +296,7 @@ class Context:
|
|||
|
||||
|
||||
CACHE_PAGES = 40
|
||||
SLOW_PAGE = 1.5 # s: cached pages built slower than this are logged
|
||||
_cache_lock = threading.Lock()
|
||||
WARM_EVERY = 60
|
||||
_code = {"t": 0, "v": None}
|
||||
|
|
@ -328,10 +334,11 @@ def _warmer(app, store):
|
|||
if p.error or not enabled(store, name):
|
||||
continue
|
||||
for path in p.warm:
|
||||
try:
|
||||
client.get(path, environ_base={"dash.internal": True}) # as admin (auth)
|
||||
except Exception as e:
|
||||
print(f"plugin {name}: warm {path}: {type(e).__name__}: {e}", flush=True)
|
||||
for v in auth.warm_viewers(): # as each admin account: the pages they get (auth.cache_key)
|
||||
try:
|
||||
client.get(path, environ_base={"dash.internal": True, "dash.viewer": v})
|
||||
except Exception as e:
|
||||
print(f"plugin {name}: warm {path}: {type(e).__name__}: {e}", flush=True)
|
||||
time.sleep(WARM_EVERY)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -452,10 +452,11 @@ def _warm(app):
|
|||
time.sleep(15) # the workers' first fetches come first
|
||||
client = app.test_client()
|
||||
for path in WARM:
|
||||
try:
|
||||
client.get(path, environ_base={"dash.internal": True}) # as admin (auth)
|
||||
except Exception as e:
|
||||
print(f"web: warm {path}: {type(e).__name__}: {e}", flush=True)
|
||||
for v in auth.warm_viewers(): # as each admin account (auth.from_request)
|
||||
try:
|
||||
client.get(path, environ_base={"dash.internal": True, "dash.viewer": v})
|
||||
except Exception as e:
|
||||
print(f"web: warm {path}: {type(e).__name__}: {e}", flush=True)
|
||||
|
||||
|
||||
def web_worker(store):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue