pig.py/neopig/live.py
Russell Ballestrini 7883ae0eba Refactor: Extract neopig package from neopig.py
Phase 1 of module restructuring as outlined in docs/REFACTOR.md:

New neopig/ package modules:
- live.py: Live media queue (get_live_queue, emit_live_media)
- state.py: AppendOnlyStateLog, state file helpers
- html_utils.py: trim_html_wrapper, extract_meta_from_html
- logging.py: TqdmLoggingHandler, job logging functions
- backfill/: markdown and screenshot backfill operations

Package features:
- Lazy import of NeoPig/main from neopig.py via __getattr__
- Full backwards compatibility with existing imports
- 42 new unit tests for extracted modules

Total: 458 tests passing
2026-01-05 17:30:58 -05:00

29 lines
899 B
Python

"""
Live media event queue for real-time SSE streaming.
Pushed to after disk save, SSE endpoint reads from here.
"""
import asyncio
from typing import Dict, Any
# Live media event queue - push here after disk save, SSE reads from here
# Format: {'md5_hash': str, 'media_type': str, 'file_size': int, 'alt_text': str, ...}
LIVE_MEDIA_QUEUE: asyncio.Queue = None # Initialized lazily
def get_live_queue() -> asyncio.Queue:
"""Get or create the live media queue."""
global LIVE_MEDIA_QUEUE
if LIVE_MEDIA_QUEUE is None:
LIVE_MEDIA_QUEUE = asyncio.Queue(maxsize=1000)
return LIVE_MEDIA_QUEUE
def emit_live_media(media_info: Dict[str, Any]):
"""Emit media to live feed (non-blocking)."""
try:
queue = get_live_queue()
queue.put_nowait(media_info)
except asyncio.QueueFull:
pass # Drop if queue is full (live feed will catch up from DB)