Prefetch SatNOGS transmitter cache at startup to fix loading delay

This commit is contained in:
James Smith
2026-03-18 15:00:03 +00:00
parent ee9bd9bbb2
commit ed1461626b
2 changed files with 34 additions and 0 deletions

8
app.py
View File

@@ -1188,6 +1188,14 @@ def _init_app() -> None:
except Exception as e:
logger.warning(f"Failed to initialize TLE auto-refresh: {e}")
# Pre-warm SatNOGS transmitter cache so first dashboard load is instant
try:
if not os.environ.get('TESTING'):
from utils.satnogs import prefetch_transmitters
prefetch_transmitters()
except Exception as e:
logger.warning(f"SatNOGS prefetch failed: {e}")
threading.Thread(target=_deferred_init, daemon=True).start()

View File

@@ -23,6 +23,7 @@ _transmitters: dict[int, list[dict]] = {}
_fetched_at: float = 0.0
_CACHE_TTL = 86400 # 24 hours in seconds
_fetch_lock = threading.Lock()
_prefetch_started = False
_SATNOGS_URL = "https://db.satnogs.org/api/transmitters/?format=json"
_REQUEST_TIMEOUT = 15 # seconds
@@ -143,3 +144,28 @@ def refresh_transmitters() -> int:
_transmitters = fetch_transmitters()
_fetched_at = time.time()
return len(_transmitters)
def prefetch_transmitters() -> None:
"""Kick off a background thread to warm the transmitter cache at startup.
Safe to call multiple times — only spawns one thread.
"""
global _prefetch_started # noqa: PLW0603
with _fetch_lock:
if _prefetch_started:
return
_prefetch_started = True
def _run() -> None:
logger.info("Pre-fetching SatNOGS transmitter data in background...")
global _transmitters, _fetched_at # noqa: PLW0603
data = fetch_transmitters()
with _fetch_lock:
_transmitters = data
_fetched_at = time.time()
logger.info("SatNOGS prefetch complete: %d satellites cached", len(data))
t = threading.Thread(target=_run, name="satnogs-prefetch", daemon=True)
t.start()