jellyfin-monitor: cleanup

This commit is contained in:
Simon Gardling 2025-09-15 12:30:28 -04:00
parent 1aec911e72
commit 13a0344db0
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -42,7 +42,7 @@ class JellyfinQBittorrentMonitor:
self.restore_normal_limits()
sys.exit(0)
def check_jellyfin_sessions(self):
def check_jellyfin_sessions(self) -> list[str]:
"""Check if anyone is actively streaming from Jellyfin"""
try:
headers = {}
@ -95,32 +95,15 @@ class JellyfinQBittorrentMonitor:
logger.error(f"SpeedLimitsMode endpoint failed: {e}")
except Exception as e:
logger.error(f"Failed to parse speedLimitsMode response: {e}")
# Fallback: try transfer info endpoint
try:
response = self.session.get(
f"{self.qbittorrent_url}/api/v2/transfer/info", timeout=10
)
if response.status_code == 200:
data = response.json()
if "use_alt_speed_limits" in data:
return data["use_alt_speed_limits"]
except Exception as e:
logger.error(f"Transfer info fallback failed: {e}")
logger.warning(
"Could not determine qBittorrent alternate limits status, using tracked state"
)
return self.throttle_active
def toggle_qbittorrent_limits(self, enable_throttle):
def use_alt_limits(self, enable: bool) -> None:
"""Toggle qBittorrent alternate speed limits"""
action = "enabled" if enable else "disabled"
try:
current_throttle = self.check_qbittorrent_alternate_limits()
if current_throttle == enable_throttle:
action = "enabled" if enable_throttle else "disabled"
if current_throttle == enable:
logger.info(
f"Alternate speed limits already {action}, no action needed"
)
@ -132,31 +115,29 @@ class JellyfinQBittorrentMonitor:
)
response.raise_for_status()
self.throttle_active = enable_throttle
self.throttle_active = enable
# Verify the change took effect
new_state = self.check_qbittorrent_alternate_limits()
if new_state == enable_throttle:
action = "enabled" if enable_throttle else "disabled"
logger.info(f"✓ Successfully {action} alternate speed limits")
if new_state == enable:
logger.info(f"Activated {action} alternate speed limits")
else:
logger.warning(
f"Toggle may have failed: expected {enable_throttle}, got {new_state}"
f"Toggle may have failed: expected {enable}, got {new_state}"
)
except requests.exceptions.RequestException as e:
action = "enable" if enable_throttle else "disable"
logger.error(f"Failed to {action} alternate speed limits: {e}")
except Exception as e:
logger.error(f"Failed to toggle qBittorrent limits: {e}")
def restore_normal_limits(self):
def restore_normal_limits(self) -> None:
"""Ensure normal speed limits are restored on shutdown"""
if self.throttle_active:
logger.info("Restoring normal speed limits before shutdown...")
self.toggle_qbittorrent_limits(False)
self.use_alt_limits(False)
def should_change_state(self, new_streaming_state):
def should_change_state(self, new_streaming_state: bool) -> bool:
"""Apply hysteresis to prevent rapid state changes"""
now = time.time()
@ -218,7 +199,7 @@ class JellyfinQBittorrentMonitor:
# Apply hysteresis and change state if needed
if self.should_change_state(streaming_active):
self.last_streaming_state = streaming_active
self.toggle_qbittorrent_limits(streaming_active)
self.use_alt_limits(streaming_active)
self.last_active_streams = active_streams
time.sleep(self.check_interval)