File size: 1,382 Bytes
3216a1f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | """
cleanup.py - Deletion of session directories on disconnect.
Provides atexit handlers and utility functions for ensuring
all session data is purged when appropriate.
"""
import atexit
from server import session_manager
from server.embedding_generator import unload_models
def _shutdown_handler():
"""Handler called on normal Python interpreter exit."""
print("[Cleanup] Server shutting down — purging all session data...")
session_manager.stop_cleanup_daemon()
session_manager.cleanup_all_sessions()
unload_models()
def register_cleanup_handlers():
"""
Register atexit handler for cleanup on shutdown.
Uvicorn handles SIGINT/SIGTERM natively — we only need atexit.
"""
atexit.register(_shutdown_handler)
print("[Cleanup] Cleanup handlers registered.")
def cleanup_session(session_id: str) -> bool:
"""
Clean up a specific session. Called on WebSocket disconnect,
browser close, or manual session end.
Args:
session_id: The session ID to clean up.
Returns:
True if the session was found and cleaned up, False otherwise.
"""
result = session_manager.end_session(session_id)
if result:
print(f"[Cleanup] Session {session_id} cleaned up successfully.")
else:
print(f"[Cleanup] Session {session_id} not found (may already be cleaned up).")
return result
|