stemserve: refresh index in a background thread

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
cdnutterandClaude Sonnet 5.5 authored and root committed 2026-11-06 14:17:38 -08:00
1 parent f099682a88
commit cf5f992e60
1 file changed
+11 -4
+11 -4
View File
@@ -93,17 +93,23 @@ class Index:
self._by_key: dict[str, dict] = {}
def get(self):
if time.monotonic() - self._at > INDEX_TTL:
self.refresh()
with self._lock:
return self._body, self._etag
def entry(self, key):
if time.monotonic() - self._at > INDEX_TTL:
self.refresh()
with self._lock:
return self._by_key.get(key)
def start_background(self):
def loop():
while True:
time.sleep(INDEX_TTL)
try:
self.refresh()
except Exception as e:
log(f"index refresh failed: {e}")
threading.Thread(target=loop, daemon=True).start()
@property
def count(self):
return self._count
@@ -537,6 +543,7 @@ def main():
sys.exit(f"STEMS_DIR not found: {STEMS_DIR}")
os.makedirs(CACHE_DIR, exist_ok=True)
INDEX.refresh()
INDEX.start_background()
log(f"stemserve on :{PORT}, {INDEX.count} tracks in {STEMS_DIR}, ffmpeg={'yes' if FFMPEG else 'no'}, "
f"token={'on' if TOKEN else 'off'}")
server = QuietServer(("0.0.0.0", PORT), Handler)