diff --git a/dashboard/auth.py b/dashboard/auth.py index 44a00da..48438be 100644 --- a/dashboard/auth.py +++ b/dashboard/auth.py @@ -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() diff --git a/dashboard/pluginhost.py b/dashboard/pluginhost.py index c470f1c..37e2816 100644 --- a/dashboard/pluginhost.py +++ b/dashboard/pluginhost.py @@ -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) diff --git a/dashboard/web.py b/dashboard/web.py index 6c22cf2..cd1375f 100644 --- a/dashboard/web.py +++ b/dashboard/web.py @@ -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):