Add function to refresh sponsors from manifest.json on startup

- Implemented `refresh_sponsors_on_startup` to load sponsors from manifest.json and update the database.
- Added error handling and logging for missing manifest file and empty sponsors list.
- Integrated the refresh function into the main application startup process.
This commit is contained in:
Admin9705
2025-06-15 12:50:16 -04:00
parent 9ae58f0180
commit 08c5765d35

38
main.py
View File

@@ -151,6 +151,37 @@ except Exception as e:
waitress_server = None
shutdown_requested = threading.Event()
def refresh_sponsors_on_startup():
"""Refresh sponsors database from manifest.json on startup"""
import os
import json
try:
# Get database instance
from src.primary.utils.database import get_database
db = get_database()
# Path to manifest.json
manifest_path = os.path.join(os.path.dirname(__file__), 'manifest.json')
if os.path.exists(manifest_path):
with open(manifest_path, 'r') as f:
manifest_data = json.load(f)
sponsors_list = manifest_data.get('sponsors', [])
if sponsors_list:
# Clear existing sponsors and save new ones
db.save_sponsors(sponsors_list)
huntarr_logger.debug(f"Refreshed {len(sponsors_list)} sponsors from manifest.json")
else:
huntarr_logger.warning("No sponsors found in manifest.json")
else:
huntarr_logger.warning(f"manifest.json not found at {manifest_path}")
except Exception as e:
huntarr_logger.error(f"Error refreshing sponsors on startup: {e}")
raise
def run_background_tasks():
"""Runs the Huntarr background processing."""
bg_logger = get_logger("HuntarrBackground") # Use app's logger
@@ -320,6 +351,13 @@ def main():
except Exception as migration_error:
huntarr_logger.warning(f"History migration completed with warnings: {migration_error}")
# Refresh sponsors from manifest.json on startup
try:
refresh_sponsors_on_startup()
huntarr_logger.info("Sponsors database refreshed from manifest.json")
except Exception as sponsor_error:
huntarr_logger.warning(f"Failed to refresh sponsors on startup: {sponsor_error}")
except Exception as e:
huntarr_logger.error(f"Failed to initialize databases: {e}")
huntarr_logger.error("Application may not function correctly without database")