mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-10-18 04:49:13 +00:00
fix(plugins): let a plugin ask to be polled faster while it has live content
Reported: "the football plugin with live games only updates the live game in
progress if I restart the display."
The data path was never the problem. NFLLiveManager fetches ESPN with no cache,
SportsLive.update() refreshes current_game in place when the game IDs are
unchanged, and the scorebug redraws from the game dict every frame -- which is
why the reporter's logs look healthy.
The problem is cadence. _get_plugin_update_interval() read only the manifest's
static update_interval, football's manifest pins that to 60, and the plugin's
own live_update_interval (15s) was invisible to the scheduler. Measured on a rig
during the fourth quarter of the game in the report:
23:21:49 23:22:50 23:23:50 23:24:50 23:25:50 <- exactly 60s apart
A clock and score up to a minute stale during a two-minute drill reads as a
frozen panel, and a restart is the one moment it is ever current.
A single static number cannot say "every 15 seconds while a game is on, every 15
minutes in July", and only the plugin knows which is true. get_update_interval()
lets it say so per tick; returning None means "no opinion" and the existing
manifest/config resolution applies, so every plugin that predates this is
unaffected.
Requests are clamped to MIN_DYNAMIC_UPDATE_INTERVAL (5s): a plugin returning 0
would otherwise be re-entered on every tick of the render loop, busy-waiting
against its own API. A hook that raises or returns a non-number is ignored
rather than propagated -- a scheduler that fails on one plugin's bug stops
updating all the others.
Deliberately NOT changed: the manifest still beats config in the static path.
That looked like the obvious fix -- user config being silently ignored -- until
checking a real rig, where football and baseball both carry update_interval 3600
in config against a manifest 60, and weather 1800 against 60. Those values are
stale precisely because nothing has been honouring them; making config win would
have slowed three plugins by 60x, turning a one-minute lag into an hour. The
dynamic hook makes the flip unnecessary. There is a test pinning the current
precedence with that reasoning attached.
Full suite: 4,283 passed, 68 skipped.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014RRtqXDCnvnY6EQwhT5CV9
This commit is contained in:
co-authored by
Claude Opus 5
parent
a29c84208e
commit
ad36d2efc3
@@ -520,6 +520,38 @@ class BasePlugin(ABC):
|
||||
"""
|
||||
return
|
||||
|
||||
def get_update_interval(self) -> Optional[float]:
|
||||
"""
|
||||
How often this plugin wants update() called, right now, in seconds.
|
||||
|
||||
The manifest's ``update_interval`` is a single static number, which
|
||||
cannot say "poll me every 15 seconds while a game is in progress and
|
||||
every 15 minutes when nothing is on". Only the plugin knows which is
|
||||
true at any moment, so override this to say so.
|
||||
|
||||
Return None (the default) to accept the manifest/config value.
|
||||
|
||||
Two constraints, both because the scheduler calls this on every tick of
|
||||
the render loop:
|
||||
|
||||
- It must be cheap. Attribute reads only -- no config lookups, no I/O,
|
||||
no locks that a fetch might be holding.
|
||||
- It must not raise. A raising hook is ignored and the static interval
|
||||
used, but a hook that raises every tick also logs every tick.
|
||||
|
||||
Values below PluginManager.MIN_DYNAMIC_UPDATE_INTERVAL are clamped up:
|
||||
a plugin asking for 0 would otherwise busy-wait against its own API.
|
||||
|
||||
Example::
|
||||
|
||||
def get_update_interval(self):
|
||||
# Fast while something is actually live, manifest default otherwise.
|
||||
if any(m.live_games for m in self._live_managers):
|
||||
return self.config.get("live_update_interval", 15)
|
||||
return None
|
||||
"""
|
||||
return None
|
||||
|
||||
def has_live_priority(self) -> bool:
|
||||
"""
|
||||
Check if this plugin has live priority enabled.
|
||||
|
||||
@@ -779,15 +779,63 @@ class PluginManager:
|
||||
|
||||
return None
|
||||
|
||||
def _dynamic_update_interval(self, plugin_id: str, plugin_instance: Any) -> Optional[float]:
|
||||
"""The interval a plugin asks for right now, or None if it has no view."""
|
||||
hook = getattr(plugin_instance, 'get_update_interval', None)
|
||||
if not callable(hook):
|
||||
return None
|
||||
try:
|
||||
requested = hook()
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
self.logger.debug(
|
||||
"get_update_interval() failed for %s, using the static interval: %s",
|
||||
plugin_id, exc)
|
||||
return None
|
||||
if requested is None:
|
||||
return None
|
||||
try:
|
||||
requested = float(requested)
|
||||
except (TypeError, ValueError):
|
||||
self.logger.debug(
|
||||
"get_update_interval() returned %r for %s, which is not a number",
|
||||
requested, plugin_id)
|
||||
return None
|
||||
if requested != requested or requested == float('inf'): # NaN / inf
|
||||
return None
|
||||
return max(requested, self.MIN_DYNAMIC_UPDATE_INTERVAL)
|
||||
|
||||
#: Floor for a plugin-requested interval. A plugin asking for 0 (or a
|
||||
#: negative) would otherwise be re-entered on every tick of the render
|
||||
#: loop, which is a busy-wait against whatever API it fetches.
|
||||
MIN_DYNAMIC_UPDATE_INTERVAL = 5.0
|
||||
|
||||
def _get_plugin_update_interval(self, plugin_id: str, plugin_instance: Any) -> Optional[float]:
|
||||
"""
|
||||
Get the data-fetch interval for a plugin (seconds between update() calls).
|
||||
|
||||
Result is cached per plugin_id after the first lookup to avoid calling
|
||||
config_manager.get_config() — which returns a full dict copy — on every
|
||||
tick of the 30-fps display loop. The cache is invalidated when a plugin
|
||||
is loaded or unloaded.
|
||||
A plugin may implement ``get_update_interval()`` to vary its own cadence
|
||||
at runtime, which the static manifest value cannot express. The case
|
||||
this exists for: a sports scoreboard needs to poll every 15s while a
|
||||
game is in progress and every 15 minutes when nothing is on, and only
|
||||
the plugin knows which is true right now. Returning None from the hook
|
||||
means "no opinion", and the static resolution below applies.
|
||||
|
||||
The hook is called on every scheduling tick, so implementations must be
|
||||
cheap — attribute reads, no config lookups and no I/O. A raising or
|
||||
non-numeric hook is ignored rather than allowed to stop the plugin
|
||||
updating, since a scheduler that propagates a plugin bug stops every
|
||||
other plugin too.
|
||||
|
||||
The static result is cached per plugin_id after the first lookup to
|
||||
avoid calling config_manager.get_config() — which returns a full dict
|
||||
copy — on every tick of the 30-fps display loop. The cache is
|
||||
invalidated when a plugin is loaded or unloaded. The dynamic hook is
|
||||
deliberately *not* cached: caching it would defeat its only purpose.
|
||||
"""
|
||||
dynamic = self._dynamic_update_interval(plugin_id, plugin_instance)
|
||||
if dynamic is not None:
|
||||
return dynamic
|
||||
|
||||
if plugin_id in self._update_interval_cache:
|
||||
return self._update_interval_cache[plugin_id]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user