mirror of
https://github.com/wizarrrr/wizarr.git
synced 2026-07-31 07:17:10 -04:00
- Added `identity_resolution.py` for resolving Wizarr users and identities. - Created `ingestion.py` to handle recording and updating activity events. - Introduced `maintenance.py` for cleanup and session management tasks. - Developed `queries.py` for read-oriented operations on activity sessions. - Implemented background tasks in `activity.py` for scheduled maintenance. - Added tests for activity services and blueprint to ensure functionality.
68 lines
2.0 KiB
Python
68 lines
2.0 KiB
Python
"""
|
|
Activity monitoring module for Wizarr.
|
|
|
|
Provides real-time activity monitoring and historical tracking of media playback
|
|
sessions across all configured media servers.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import threading
|
|
|
|
import structlog
|
|
from flask import Flask
|
|
|
|
from app.models import ActivitySession, ActivitySnapshot
|
|
from app.services.activity import ActivityService
|
|
|
|
from .monitoring.monitor import WebSocketMonitor
|
|
|
|
|
|
def init_app(app: Flask) -> None:
|
|
"""Initialise activity monitoring features with the Flask application."""
|
|
logger = structlog.get_logger(__name__)
|
|
|
|
# Skip activity monitoring during tests
|
|
if app.config.get("TESTING"):
|
|
logger.debug("Skipping activity monitoring in test mode")
|
|
return
|
|
|
|
if getattr(app, "debug", False) and os.environ.get("WERKZEUG_RUN_MAIN") != "true":
|
|
logger.debug("Skipping activity monitoring in reloader parent process")
|
|
return
|
|
|
|
app.extensions = getattr(app, "extensions", {})
|
|
if "activity_monitor" in app.extensions:
|
|
logger.debug("Activity monitoring already initialized, skipping")
|
|
return
|
|
|
|
logger.info("Initializing activity monitoring")
|
|
monitor = WebSocketMonitor(app)
|
|
app.extensions["activity_monitor"] = monitor
|
|
|
|
def delayed_start():
|
|
import time
|
|
|
|
time.sleep(2)
|
|
|
|
try:
|
|
from app.tasks.activity import recover_sessions_on_startup_task
|
|
|
|
recovered_count = recover_sessions_on_startup_task(app)
|
|
logger.info(
|
|
"Session recovery completed on startup: %s orphaned sessions cleaned up",
|
|
recovered_count,
|
|
)
|
|
except Exception as exc:
|
|
logger.error("Session recovery failed on startup: %s", exc, exc_info=True)
|
|
|
|
monitor.start_monitoring()
|
|
|
|
threading.Thread(target=delayed_start, daemon=True).start()
|
|
|
|
logger.info("Activity monitoring initialized")
|
|
|
|
|
|
__all__ = ["ActivitySession", "ActivitySnapshot", "ActivityService", "init_app"]
|