5b7a55a05d
- ExportRunner: stop batch on first error (was continuing, overwriting error status with done) - Export route: validate input_path against MEDIA_DIRS - Export route: validate encoder, portrait_ratio, folder_suffix, name - Export route: fix format check for WebP sequence - Export route: add _ separator in folder_suffix (match GUI) - Export route: use realpath consistently in delete endpoint - Export route: drop runner ref on completion (prevent memory leak) - ProcessedDB: use cursor-level row_factory (thread-safe) - WebSocket: catch all exceptions in connect, cleanup in finally - Dockerfile: use uvicorn[standard] for websockets support Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import asyncio
|
|
import json
|
|
import threading
|
|
|
|
from fastapi import WebSocket, WebSocketDisconnect
|
|
|
|
_lock = threading.Lock()
|
|
_connections: list[WebSocket] = []
|
|
_loop: asyncio.AbstractEventLoop | None = None
|
|
|
|
|
|
async def connect(ws: WebSocket):
|
|
global _loop
|
|
_loop = asyncio.get_running_loop()
|
|
await ws.accept()
|
|
with _lock:
|
|
_connections.append(ws)
|
|
try:
|
|
while True:
|
|
await ws.receive_text() # keep alive
|
|
except (WebSocketDisconnect, Exception):
|
|
pass
|
|
finally:
|
|
with _lock:
|
|
if ws in _connections:
|
|
_connections.remove(ws)
|
|
|
|
|
|
def broadcast(msg: dict):
|
|
"""Send a message to all connected WebSocket clients.
|
|
|
|
Called from sync code (export callbacks running in background threads),
|
|
so we schedule sends on uvicorn's event loop.
|
|
"""
|
|
if _loop is None:
|
|
return
|
|
data = json.dumps(msg)
|
|
with _lock:
|
|
for ws in list(_connections):
|
|
try:
|
|
asyncio.run_coroutine_threadsafe(ws.send_text(data), _loop)
|
|
except Exception:
|
|
pass
|