JAYNESTV commited on
Commit
9c1bfbd
·
verified ·
1 Parent(s): b141afe

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -370,11 +370,19 @@ def warm_loop():
370
  return
371
  time.sleep(5)
372
 
 
 
373
  def _warm_one(n):
 
 
374
  try:
375
  url, msg = ui_start_channel(n)
376
  if not url:
377
- log.warning("WARM FAIL %s: %s", n, str(msg)[:300])
 
 
 
 
378
  sid = url.split("/hls/")[1].split("/")[0] if "/hls/" in url else None
379
  if sid and sid in SESSIONS:
380
  SESSIONS[sid]["last"] = time.time()
@@ -578,12 +586,13 @@ def ui_refresh():
578
  return gr.update(choices=list(CHANNELS)), f"✅ Channels: {len(CHANNELS)}"
579
 
580
 
581
- def ui_start_channel(name: str) -> tuple[str, str]:
582
  cid = CHANNELS.get(name)
583
  if not cid:
584
  return "", "❌ Chagua channel kwanza"
585
  hit = DETAIL_CACHE.get(cid)
586
- if hit and time.time() - hit[0] < CACHE_TTL:
 
587
  mpd, key = hit[1]
588
  else:
589
  try:
@@ -597,7 +606,12 @@ def ui_start_channel(name: str) -> tuple[str, str]:
597
  fields = list(detail.keys()) if isinstance(detail, dict) else type(detail).__name__
598
  return "", f"❌ Sikupata MPD kwenye jibu la channel. Fields: {fields}"
599
  DETAIL_CACHE[cid] = (time.time(), (mpd, key))
600
- return ui_start(mpd, key, name)
 
 
 
 
 
601
 
602
 
603
  def health_check() -> str:
 
370
  return
371
  time.sleep(5)
372
 
373
+ fails, nxt = {}, {}
374
+
375
  def _warm_one(n):
376
+ if nxt.get(n, 0) > time.time():
377
+ return
378
  try:
379
  url, msg = ui_start_channel(n)
380
  if not url:
381
+ fails[n] = fails.get(n, 0) + 1
382
+ nxt[n] = time.time() + min(300, 20 * 2 ** fails[n]) # backoff: 40s, 80s ... 5min
383
+ log.warning("WARM FAIL %s (#%d): %s", n, fails[n], str(msg)[:300])
384
+ else:
385
+ fails.pop(n, None)
386
  sid = url.split("/hls/")[1].split("/")[0] if "/hls/" in url else None
387
  if sid and sid in SESSIONS:
388
  SESSIONS[sid]["last"] = time.time()
 
586
  return gr.update(choices=list(CHANNELS)), f"✅ Channels: {len(CHANNELS)}"
587
 
588
 
589
+ def ui_start_channel(name: str, _retry: bool = True) -> tuple[str, str]:
590
  cid = CHANNELS.get(name)
591
  if not cid:
592
  return "", "❌ Chagua channel kwanza"
593
  hit = DETAIL_CACHE.get(cid)
594
+ cached = bool(hit and time.time() - hit[0] < CACHE_TTL)
595
+ if cached:
596
  mpd, key = hit[1]
597
  else:
598
  try:
 
606
  fields = list(detail.keys()) if isinstance(detail, dict) else type(detail).__name__
607
  return "", f"❌ Sikupata MPD kwenye jibu la channel. Fields: {fields}"
608
  DETAIL_CACHE[cid] = (time.time(), (mpd, key))
609
+ res = ui_start(mpd, key, name)
610
+ if not res[0] and cached and _retry: # token/MPD ya zamani? chukua mpya mara moja
611
+ DETAIL_CACHE.pop(cid, None)
612
+ PLAN_CACHE.pop(f"{mpd}|{clean_hex(key)}", None)
613
+ return ui_start_channel(name, _retry=False)
614
+ return res
615
 
616
 
617
  def health_check() -> str: