jellyfin-monitor: cleanup
This commit is contained in:
parent
1aec911e72
commit
13a0344db0
@ -42,7 +42,7 @@ class JellyfinQBittorrentMonitor:
|
|||||||
self.restore_normal_limits()
|
self.restore_normal_limits()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def check_jellyfin_sessions(self):
|
def check_jellyfin_sessions(self) -> list[str]:
|
||||||
"""Check if anyone is actively streaming from Jellyfin"""
|
"""Check if anyone is actively streaming from Jellyfin"""
|
||||||
try:
|
try:
|
||||||
headers = {}
|
headers = {}
|
||||||
@ -95,32 +95,15 @@ class JellyfinQBittorrentMonitor:
|
|||||||
logger.error(f"SpeedLimitsMode endpoint failed: {e}")
|
logger.error(f"SpeedLimitsMode endpoint failed: {e}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to parse speedLimitsMode response: {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
|
return self.throttle_active
|
||||||
|
|
||||||
def toggle_qbittorrent_limits(self, enable_throttle):
|
def use_alt_limits(self, enable: bool) -> None:
|
||||||
"""Toggle qBittorrent alternate speed limits"""
|
"""Toggle qBittorrent alternate speed limits"""
|
||||||
|
action = "enabled" if enable else "disabled"
|
||||||
try:
|
try:
|
||||||
current_throttle = self.check_qbittorrent_alternate_limits()
|
current_throttle = self.check_qbittorrent_alternate_limits()
|
||||||
|
|
||||||
if current_throttle == enable_throttle:
|
if current_throttle == enable:
|
||||||
action = "enabled" if enable_throttle else "disabled"
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Alternate speed limits already {action}, no action needed"
|
f"Alternate speed limits already {action}, no action needed"
|
||||||
)
|
)
|
||||||
@ -132,31 +115,29 @@ class JellyfinQBittorrentMonitor:
|
|||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
self.throttle_active = enable_throttle
|
self.throttle_active = enable
|
||||||
|
|
||||||
# Verify the change took effect
|
# Verify the change took effect
|
||||||
new_state = self.check_qbittorrent_alternate_limits()
|
new_state = self.check_qbittorrent_alternate_limits()
|
||||||
if new_state == enable_throttle:
|
if new_state == enable:
|
||||||
action = "enabled" if enable_throttle else "disabled"
|
logger.info(f"Activated {action} alternate speed limits")
|
||||||
logger.info(f"✓ Successfully {action} alternate speed limits")
|
|
||||||
else:
|
else:
|
||||||
logger.warning(
|
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:
|
except requests.exceptions.RequestException as e:
|
||||||
action = "enable" if enable_throttle else "disable"
|
|
||||||
logger.error(f"Failed to {action} alternate speed limits: {e}")
|
logger.error(f"Failed to {action} alternate speed limits: {e}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to toggle qBittorrent limits: {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"""
|
"""Ensure normal speed limits are restored on shutdown"""
|
||||||
if self.throttle_active:
|
if self.throttle_active:
|
||||||
logger.info("Restoring normal speed limits before shutdown...")
|
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"""
|
"""Apply hysteresis to prevent rapid state changes"""
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
|
||||||
@ -218,7 +199,7 @@ class JellyfinQBittorrentMonitor:
|
|||||||
# Apply hysteresis and change state if needed
|
# Apply hysteresis and change state if needed
|
||||||
if self.should_change_state(streaming_active):
|
if self.should_change_state(streaming_active):
|
||||||
self.last_streaming_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
|
self.last_active_streams = active_streams
|
||||||
time.sleep(self.check_interval)
|
time.sleep(self.check_interval)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user