Sasha commited on
Commit
fe2d35c
·
1 Parent(s): c48eed2

feat: throttle VOD resolution checks to 5m and auto-complete streams older than 3 hours with no VOD

Browse files
local_worker/chat_mod_worker.py CHANGED
@@ -1,7 +1,8 @@
1
- from datetime import datetime, timedelta
2
  import os
3
  import sys
4
  import time
 
5
  import socket
6
  import select
7
  import threading
@@ -447,9 +448,19 @@ def resolve_missing_vods():
447
  for stream in streams:
448
  stream_id = stream.get("id")
449
  start_time_str = stream.get("start_time")
 
450
  if not stream_id or not start_time_str:
451
  continue
452
 
 
 
 
 
 
 
 
 
 
453
  matched_vod_id = find_matching_vod(start_time_str, recent_vods)
454
  if matched_vod_id:
455
  print(f"[VOD Resolve] Stream ID {stream_id} ({start_time_str}) matched with Twitch VOD {matched_vod_id}.")
@@ -463,7 +474,14 @@ def resolve_missing_vods():
463
  else:
464
  print(f"[VOD Resolve] Failed to update VOD ID: {up_res.status_code}")
465
  else:
466
- print(f"[VOD Resolve] No matching VOD found for stream ID {stream_id} ({start_time_str}) within timeframe.")
 
 
 
 
 
 
 
467
  except Exception as e:
468
  print(f"[VOD Resolve] Exception during resolve loop: {e}")
469
 
@@ -490,8 +508,12 @@ def run_backfill_loop():
490
  """Main loop to check and backfill VOD chat comments ONLY"""
491
 
492
  while not stop_flag.is_set():
493
- # Try to resolve any missing VOD IDs before checking pending backfills
494
- resolve_missing_vods()
 
 
 
 
495
  print(f"[Backfill] Checking for pending VOD backfill tasks...")
496
  processed_streams = load_processed_streams()
497
 
 
1
+ from datetime import datetime, timedelta, timezone
2
  import os
3
  import sys
4
  import time
5
+ last_resolve_time = 0
6
  import socket
7
  import select
8
  import threading
 
448
  for stream in streams:
449
  stream_id = stream.get("id")
450
  start_time_str = stream.get("start_time")
451
+ end_time_str = stream.get("end_time")
452
  if not stream_id or not start_time_str:
453
  continue
454
 
455
+ # Calculate stream age from end_time
456
+ try:
457
+ clean_end = end_time_str.replace('Z', '+00:00') if end_time_str else start_time_str.replace('Z', '+00:00')
458
+ end_dt = datetime.fromisoformat(clean_end)
459
+ now_utc = datetime.now(timezone.utc)
460
+ age_hours = (now_utc - end_dt).total_seconds() / 3600.0
461
+ except Exception as age_err:
462
+ age_hours = 0
463
+
464
  matched_vod_id = find_matching_vod(start_time_str, recent_vods)
465
  if matched_vod_id:
466
  print(f"[VOD Resolve] Stream ID {stream_id} ({start_time_str}) matched with Twitch VOD {matched_vod_id}.")
 
474
  else:
475
  print(f"[VOD Resolve] Failed to update VOD ID: {up_res.status_code}")
476
  else:
477
+ if age_hours > 3.0:
478
+ print(f"[VOD Resolve] Stream ID {stream_id} is older than 3 hours and has no matching VOD. Marking as completed to clear queue.")
479
+ try:
480
+ requests.post(f"{API_URL}/api/streams/mark-backfilled", json={"streamId": stream_id}, headers=headers, timeout=10)
481
+ except Exception as mark_e:
482
+ print(f"[VOD Resolve] Error marking stream as completed: {mark_e}")
483
+ else:
484
+ print(f"[VOD Resolve] No matching VOD found for stream ID {stream_id} ({start_time_str}) within timeframe (will retry).")
485
  except Exception as e:
486
  print(f"[VOD Resolve] Exception during resolve loop: {e}")
487
 
 
508
  """Main loop to check and backfill VOD chat comments ONLY"""
509
 
510
  while not stop_flag.is_set():
511
+ # Try to resolve any missing VOD IDs once every 5 minutes (300s) to avoid log spam
512
+ global last_resolve_time
513
+ now_sec = time.time()
514
+ if now_sec - last_resolve_time > 300:
515
+ resolve_missing_vods()
516
+ last_resolve_time = now_sec
517
  print(f"[Backfill] Checking for pending VOD backfill tasks...")
518
  processed_streams = load_processed_streams()
519
 
local_worker/worker.py CHANGED
@@ -21,8 +21,9 @@ if added_paths:
21
  else:
22
  os.environ["LD_LIBRARY_PATH"] = ":".join(added_paths)
23
 
24
- from datetime import datetime, timedelta
25
  import time
 
26
  import socket
27
  import select
28
  import threading
@@ -413,9 +414,19 @@ def resolve_missing_vods():
413
  for stream in streams:
414
  stream_id = stream.get("id")
415
  start_time_str = stream.get("start_time")
 
416
  if not stream_id or not start_time_str:
417
  continue
418
 
 
 
 
 
 
 
 
 
 
419
  matched_vod_id = find_matching_vod(start_time_str, recent_vods)
420
  if matched_vod_id:
421
  print(f"[VOD Resolve] Stream ID {stream_id} ({start_time_str}) matched with Twitch VOD {matched_vod_id}.")
@@ -429,7 +440,14 @@ def resolve_missing_vods():
429
  else:
430
  print(f"[VOD Resolve] Failed to update VOD ID: {up_res.status_code}")
431
  else:
432
- print(f"[VOD Resolve] No matching VOD found for stream ID {stream_id} ({start_time_str}) within timeframe.")
 
 
 
 
 
 
 
433
  except Exception as e:
434
  print(f"[VOD Resolve] Exception during resolve loop: {e}")
435
 
@@ -532,8 +550,12 @@ def run_stream_capture(model):
532
 
533
  while not stop_flag.is_set():
534
  if not check_stream_live():
535
- # Try to resolve any missing VOD IDs before checking pending backfills
536
- resolve_missing_vods()
 
 
 
 
537
  print(f"[Capture] Channel '{TWITCH_CHANNEL}' is offline. Checking for pending VOD backfill tasks...")
538
  try:
539
  headers_get = {"x-api-key": API_KEY}
 
21
  else:
22
  os.environ["LD_LIBRARY_PATH"] = ":".join(added_paths)
23
 
24
+ from datetime import datetime, timedelta, timezone
25
  import time
26
+ last_resolve_time = 0
27
  import socket
28
  import select
29
  import threading
 
414
  for stream in streams:
415
  stream_id = stream.get("id")
416
  start_time_str = stream.get("start_time")
417
+ end_time_str = stream.get("end_time")
418
  if not stream_id or not start_time_str:
419
  continue
420
 
421
+ # Calculate stream age from end_time
422
+ try:
423
+ clean_end = end_time_str.replace('Z', '+00:00') if end_time_str else start_time_str.replace('Z', '+00:00')
424
+ end_dt = datetime.fromisoformat(clean_end)
425
+ now_utc = datetime.now(timezone.utc)
426
+ age_hours = (now_utc - end_dt).total_seconds() / 3600.0
427
+ except Exception as age_err:
428
+ age_hours = 0
429
+
430
  matched_vod_id = find_matching_vod(start_time_str, recent_vods)
431
  if matched_vod_id:
432
  print(f"[VOD Resolve] Stream ID {stream_id} ({start_time_str}) matched with Twitch VOD {matched_vod_id}.")
 
440
  else:
441
  print(f"[VOD Resolve] Failed to update VOD ID: {up_res.status_code}")
442
  else:
443
+ if age_hours > 3.0:
444
+ print(f"[VOD Resolve] Stream ID {stream_id} is older than 3 hours and has no matching VOD. Marking as completed to clear queue.")
445
+ try:
446
+ requests.post(f"{API_URL}/api/streams/mark-backfilled", json={"streamId": stream_id}, headers=headers, timeout=10)
447
+ except Exception as mark_e:
448
+ print(f"[VOD Resolve] Error marking stream as completed: {mark_e}")
449
+ else:
450
+ print(f"[VOD Resolve] No matching VOD found for stream ID {stream_id} ({start_time_str}) within timeframe (will retry).")
451
  except Exception as e:
452
  print(f"[VOD Resolve] Exception during resolve loop: {e}")
453
 
 
550
 
551
  while not stop_flag.is_set():
552
  if not check_stream_live():
553
+ # Try to resolve any missing VOD IDs once every 5 minutes (300s) to avoid log spam
554
+ global last_resolve_time
555
+ now_sec = time.time()
556
+ if now_sec - last_resolve_time > 300:
557
+ resolve_missing_vods()
558
+ last_resolve_time = now_sec
559
  print(f"[Capture] Channel '{TWITCH_CHANNEL}' is offline. Checking for pending VOD backfill tasks...")
560
  try:
561
  headers_get = {"x-api-key": API_KEY}