This commit is contained in:
Admin9705
2025-05-02 16:04:43 -04:00
parent 899f50012f
commit 523d7fd942
4 changed files with 60 additions and 47 deletions
+4 -3
View File
@@ -13,6 +13,7 @@ from src.primary.apps.radarr import api as radarr_api
from src.primary.stats_manager import increment_stat
from src.primary.stateful_manager import is_processed, add_processed_id
from src.primary.utils.history_utils import log_processed_media
from src.primary.settings_manager import load_settings, get_advanced_setting
# Get logger for the app
radarr_logger = get_logger("radarr")
@@ -37,13 +38,13 @@ def process_missing_movies(
# Extract necessary settings
api_url = app_settings.get("api_url")
api_key = app_settings.get("api_key")
api_timeout = app_settings.get("api_timeout", 90) # Default timeout
api_timeout = get_advanced_setting("api_timeout", 90) # Default timeout
monitored_only = app_settings.get("monitored_only", True)
skip_future_releases = app_settings.get("skip_future_releases", True)
skip_movie_refresh = app_settings.get("skip_movie_refresh", False)
hunt_missing_movies = app_settings.get("hunt_missing_movies", 0)
command_wait_delay = app_settings.get("command_wait_delay", 5)
command_wait_attempts = app_settings.get("command_wait_attempts", 12)
command_wait_delay = get_advanced_setting("command_wait_delay", 5)
command_wait_attempts = get_advanced_setting("command_wait_attempts", 12)
instance_name = app_settings.get("name", "Default")
if not api_url or not api_key:
+4 -3
View File
@@ -11,6 +11,7 @@ from src.primary.apps.sonarr import api as sonarr_api
from src.primary.stats_manager import increment_stat
from src.primary.stateful_manager import is_processed, add_processed_id
from src.primary.utils.history_utils import log_processed_media
from src.primary.settings_manager import load_settings, get_advanced_setting
# Get logger for the Sonarr app
sonarr_logger = get_logger("sonarr")
@@ -19,14 +20,14 @@ def process_missing_episodes(
api_url: str,
api_key: str,
instance_name: str = "Default",
api_timeout: int = 60,
api_timeout: int = get_advanced_setting("api_timeout", 90),
monitored_only: bool = True,
skip_future_episodes: bool = True,
skip_series_refresh: bool = False,
hunt_missing_items: int = 5,
hunt_missing_mode: str = "episodes",
command_wait_delay: int = 5,
command_wait_attempts: int = 10,
command_wait_delay: int = get_advanced_setting("command_wait_delay", 5),
command_wait_attempts: int = get_advanced_setting("command_wait_attempts", 12),
stop_check: Callable[[], bool] = lambda: False
) -> bool:
"""
+31
View File
@@ -272,6 +272,37 @@ def apply_timezone(timezone: str) -> bool:
settings_logger.error(f"Error setting timezone: {str(e)}")
return False
# Add a list of known advanced settings for clarity and documentation
ADVANCED_SETTINGS = [
"api_timeout",
"command_wait_delay",
"command_wait_attempts",
"minimum_download_queue_size",
"log_refresh_interval_seconds"
]
def get_advanced_setting(setting_name, default_value=None):
"""
Get an advanced setting from general settings.
Advanced settings are now centralized in general settings and no longer stored
in individual app settings files. This function provides a consistent way to
access these settings from anywhere in the codebase.
Args:
setting_name: The name of the advanced setting to retrieve
default_value: The default value to return if the setting is not found
Returns:
The value of the setting or the default value if not found
"""
if setting_name not in ADVANCED_SETTINGS:
settings_logger.warning(f"Requested unknown advanced setting: {setting_name}")
# Get from general settings
general_settings = load_settings('general', use_cache=True)
return general_settings.get(setting_name, default_value)
# Example usage (for testing purposes, remove later)
if __name__ == "__main__":
settings_logger.info(f"Known app types: {KNOWN_APP_TYPES}")
+21 -41
View File
@@ -32,6 +32,9 @@ APP_TYPES = ["sonarr", "radarr", "lidarr", "readarr", "whisparr"]
for app_type in APP_TYPES:
(STATEFUL_DIR / app_type).mkdir(exist_ok=True)
# Add import for get_advanced_setting
from src.primary.settings_manager import get_advanced_setting
def initialize_lock_file() -> None:
"""Initialize the lock file with the current timestamp if it doesn't exist."""
# Ensure directory exists - we don't need to log this again
@@ -44,21 +47,16 @@ def initialize_lock_file() -> None:
try:
current_time = int(time.time())
# Get the expiration hours setting
try:
from src.primary.settings_manager import get_setting
hours = get_setting("general", "stateful_management_hours", DEFAULT_HOURS)
except Exception as e:
stateful_logger.error(f"Error getting stateful hours setting, using default: {e}")
hours = DEFAULT_HOURS
expires_at = current_time + (hours * 3600)
expiration_hours = get_advanced_setting("statefulExpirationHours", DEFAULT_HOURS)
expires_at = current_time + (expiration_hours * 3600)
with open(LOCK_FILE, 'w') as f:
json.dump({
"created_at": current_time,
"expires_at": expires_at
}, f, indent=2)
stateful_logger.info(f"Initialized lock file at {LOCK_FILE} with expiration in {hours} hours")
stateful_logger.info(f"Initialized lock file at {LOCK_FILE} with expiration in {expiration_hours} hours")
except Exception as e:
stateful_logger.error(f"Error initializing lock file: {e}")
@@ -78,9 +76,8 @@ def get_lock_info() -> Dict[str, Any]:
if "expires_at" not in lock_info or lock_info["expires_at"] is None:
# Recalculate expiration if missing
from src.primary.settings_manager import get_setting
hours = get_setting("general", "stateful_management_hours", DEFAULT_HOURS)
lock_info["expires_at"] = lock_info["created_at"] + (hours * 3600)
expiration_hours = get_advanced_setting("statefulExpirationHours", DEFAULT_HOURS)
lock_info["expires_at"] = lock_info["created_at"] + (expiration_hours * 3600)
# Save the updated info
with open(LOCK_FILE, 'w') as f:
@@ -91,13 +88,8 @@ def get_lock_info() -> Dict[str, Any]:
stateful_logger.error(f"Error reading lock file: {e}")
# Return default values if there's an error
current_time = int(time.time())
try:
from src.primary.settings_manager import get_setting
hours = get_setting("general", "stateful_management_hours", DEFAULT_HOURS)
except:
hours = DEFAULT_HOURS
expires_at = current_time + (hours * 3600)
expiration_hours = get_advanced_setting("statefulExpirationHours", DEFAULT_HOURS)
expires_at = current_time + (expiration_hours * 3600)
return {
"created_at": current_time,
@@ -107,12 +99,11 @@ def get_lock_info() -> Dict[str, Any]:
def update_lock_expiration(hours: int = None) -> None:
"""Update the lock expiration based on the hours setting."""
if hours is None:
from src.primary.settings_manager import get_setting
hours = get_setting("general", "stateful_management_hours", DEFAULT_HOURS)
expiration_hours = get_advanced_setting("statefulExpirationHours", DEFAULT_HOURS)
lock_info = get_lock_info()
created_at = lock_info.get("created_at", int(time.time()))
expires_at = created_at + (hours * 3600)
expires_at = created_at + (expiration_hours * 3600)
lock_info["expires_at"] = expires_at
@@ -138,16 +129,11 @@ def reset_stateful_management() -> bool:
"""
try:
# Get the expiration hours setting BEFORE writing the lock file
try:
from src.primary.settings_manager import get_setting
hours = get_setting("general", "stateful_management_hours", DEFAULT_HOURS)
except Exception as e:
stateful_logger.error(f"Error getting stateful hours setting during reset, using default: {e}")
hours = DEFAULT_HOURS
expiration_hours = get_advanced_setting("statefulExpirationHours", DEFAULT_HOURS)
# Create new lock file with calculated expiration
current_time = int(time.time())
expires_at = current_time + (hours * 3600)
expires_at = current_time + (expiration_hours * 3600)
with open(LOCK_FILE, 'w') as f:
json.dump({
@@ -318,17 +304,12 @@ def get_stateful_management_info() -> Dict[str, Any]:
expires_at_ts = lock_info.get("expires_at")
# Get the interval setting
try:
from src.primary.settings_manager import get_setting
hours_interval = get_setting("general", "stateful_management_hours", DEFAULT_HOURS)
except Exception as e:
stateful_logger.error(f"Error getting stateful hours setting, using default: {e}")
hours_interval = DEFAULT_HOURS
expiration_hours = get_advanced_setting("statefulExpirationHours", DEFAULT_HOURS)
return {
"created_at_ts": created_at_ts,
"expires_at_ts": expires_at_ts,
"interval_hours": hours_interval
"interval_hours": expiration_hours
}
def initialize_stateful_system():
@@ -348,10 +329,9 @@ def initialize_stateful_system():
try:
initialize_lock_file()
# Update expiration time
from src.primary.settings_manager import get_setting
hours = get_setting("general", "stateful_management_hours", DEFAULT_HOURS)
update_lock_expiration(hours)
stateful_logger.info(f"Stateful lock file initialized with {hours} hour expiration")
expiration_hours = get_advanced_setting("statefulExpirationHours", DEFAULT_HOURS)
update_lock_expiration(expiration_hours)
stateful_logger.info(f"Stateful lock file initialized with {expiration_hours} hour expiration")
except Exception as e:
stateful_logger.error(f"Failed to initialize lock file: {e}")