From 3ab44674234f85903ea266bd705c3918b1a11af3 Mon Sep 17 00:00:00 2001 From: Lukas Pupka-Lipinski Date: Mon, 29 Sep 2025 14:19:29 +0200 Subject: [PATCH] fix duplication run --- src/Main.py | 10 +++- src/server/app.py | 54 ++++++++++--------- src/server/logs/aniworld.log | 82 +++++++++++++++++++++++++++++ src/server/web/routes/api_routes.py | 39 ++++++++++---- 4 files changed, 148 insertions(+), 37 deletions(-) diff --git a/src/Main.py b/src/Main.py index 53e6fb6..98efc76 100644 --- a/src/Main.py +++ b/src/Main.py @@ -36,9 +36,15 @@ class MatchNotFoundError(Exception): class SeriesApp: + _initialization_count = 0 # Track how many times initialization has been called + def __init__(self, directory_to_search: str): - - print("Please wait while initializing...") + SeriesApp._initialization_count += 1 + + # Only show initialization message for the first instance + if SeriesApp._initialization_count <= 1: + print("Please wait while initializing...") + self.progress = None self.directory_to_search = directory_to_search self.Loaders = Loaders() diff --git a/src/server/app.py b/src/server/app.py index ae436f5..9e37fa2 100644 --- a/src/server/app.py +++ b/src/server/app.py @@ -177,9 +177,6 @@ def init_series_app(): series_app = SeriesApp(directory_to_search) return series_app -# Initialize the app on startup -init_series_app() - # Register WebSocket handlers register_socketio_handlers(socketio) @@ -288,30 +285,37 @@ def cleanup_on_exit(): # All routes have been moved to separate blueprint files in web/routes/ if __name__ == '__main__': + # Only run initialization and logging setup in the main process + # This prevents duplicate initialization when Flask debug reloader starts - # Configure enhanced logging system - try: - from server.infrastructure.logging.config import get_logger, logging_config - logger = get_logger(__name__, 'webapp') - logger.info("Enhanced logging system initialized") - except ImportError: - # Fallback to basic logging - logging.basicConfig(level=logging.INFO) - logger = logging.getLogger(__name__) - logger.warning("Using fallback logging - enhanced logging not available") + # Initialize the series app + init_series_app() - logger.info("Starting Aniworld Flask server...") - logger.info(f"Anime directory: {config.anime_directory}") - logger.info(f"Log level: {config.log_level}") - - # Start scheduler if enabled - # if config.scheduled_rescan_enabled: - # logger.info(f"Starting scheduler - daily rescan at {config.scheduled_rescan_time}") - # scheduler.start_scheduler() - # else: - logger.info("Scheduled operations disabled") - - logger.info("Server will be available at http://localhost:5000") + # Only configure logging if we're not in the reloader child process + if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': + # Configure enhanced logging system + try: + from server.infrastructure.logging.config import get_logger, logging_config + logger = get_logger(__name__, 'webapp') + logger.info("Enhanced logging system initialized") + except ImportError: + # Fallback to basic logging + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__name__) + logger.warning("Using fallback logging - enhanced logging not available") + + logger.info("Starting Aniworld Flask server...") + logger.info(f"Anime directory: {config.anime_directory}") + logger.info(f"Log level: {config.log_level}") + + # Start scheduler if enabled + # if config.scheduled_rescan_enabled: + # logger.info(f"Starting scheduler - daily rescan at {config.scheduled_rescan_time}") + # scheduler.start_scheduler() + # else: + logger.info("Scheduled operations disabled") + + logger.info("Server will be available at http://localhost:5000") try: # Run with SocketIO diff --git a/src/server/logs/aniworld.log b/src/server/logs/aniworld.log index aecee72..2f53aa2 100644 --- a/src/server/logs/aniworld.log +++ b/src/server/logs/aniworld.log @@ -195,3 +195,85 @@ 2025-09-29 14:09:28 - WARNING - werkzeug - _log - * Debugger is active! 2025-09-29 14:09:47 - INFO - root - cleanup_on_exit - Application cleanup completed 2025-09-29 14:09:48 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:15:55 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:15:55 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:15:55 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:15:55 - INFO - __main__ - - Log level: INFO +2025-09-29 14:15:55 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:15:55 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:16:02 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:16:02 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:16:02 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:16:02 - INFO - __main__ - - Log level: INFO +2025-09-29 14:16:02 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:16:02 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:16:02 - WARNING - werkzeug - _log - * Debugger is active! +2025-09-29 14:16:24 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:16:28 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:16:28 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:16:28 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:16:28 - INFO - __main__ - - Log level: INFO +2025-09-29 14:16:28 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:16:28 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:16:28 - WARNING - werkzeug - _log - * Debugger is active! +2025-09-29 14:16:42 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:16:47 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:16:47 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:16:47 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:16:47 - INFO - __main__ - - Log level: INFO +2025-09-29 14:16:47 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:16:47 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:16:47 - WARNING - werkzeug - _log - * Debugger is active! +2025-09-29 14:17:01 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:17:05 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:17:05 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:17:05 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:17:05 - INFO - __main__ - - Log level: INFO +2025-09-29 14:17:05 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:17:05 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:17:05 - WARNING - werkzeug - _log - * Debugger is active! +2025-09-29 14:17:11 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:17:15 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:17:15 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:17:15 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:17:15 - INFO - __main__ - - Log level: INFO +2025-09-29 14:17:15 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:17:15 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:17:15 - WARNING - werkzeug - _log - * Debugger is active! +2025-09-29 14:17:24 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:17:29 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:17:29 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:17:29 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:17:29 - INFO - __main__ - - Log level: INFO +2025-09-29 14:17:29 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:17:29 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:17:29 - WARNING - werkzeug - _log - * Debugger is active! +2025-09-29 14:17:38 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:17:38 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:17:38 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:17:38 - INFO - __main__ - - Log level: INFO +2025-09-29 14:17:38 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:17:38 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:17:42 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:17:42 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:17:42 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:17:42 - INFO - __main__ - - Log level: INFO +2025-09-29 14:17:42 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:17:42 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:17:42 - WARNING - werkzeug - _log - * Debugger is active! +2025-09-29 14:18:02 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:18:03 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:18:16 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:18:16 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:18:16 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:18:16 - INFO - __main__ - - Log level: INFO +2025-09-29 14:18:16 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:18:16 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:18:57 - INFO - root - cleanup_on_exit - Application cleanup completed +2025-09-29 14:19:04 - INFO - __main__ - - Enhanced logging system initialized +2025-09-29 14:19:04 - INFO - __main__ - - Starting Aniworld Flask server... +2025-09-29 14:19:04 - INFO - __main__ - - Anime directory: \\sshfs.r\ubuntu@192.168.178.43\media\serien\Serien +2025-09-29 14:19:04 - INFO - __main__ - - Log level: INFO +2025-09-29 14:19:04 - INFO - __main__ - - Scheduled operations disabled +2025-09-29 14:19:04 - INFO - __main__ - - Server will be available at http://localhost:5000 +2025-09-29 14:19:06 - INFO - root - cleanup_on_exit - Application cleanup completed diff --git a/src/server/web/routes/api_routes.py b/src/server/web/routes/api_routes.py index 2e02bec..fcaf6d7 100644 --- a/src/server/web/routes/api_routes.py +++ b/src/server/web/routes/api_routes.py @@ -84,6 +84,22 @@ def init_series_app(): series_app = SeriesApp(directory_to_search) return series_app +def get_series_app(): + """Get the current series app instance, initializing if needed.""" + global series_app + if series_app is None: + # Try to get it from the main app module first + try: + import app + if hasattr(app, 'series_app') and app.series_app is not None: + series_app = app.series_app + return series_app + except ImportError: + pass + # If not available, initialize it + init_series_app() + return series_app + # Import socketio instance - this will need to be passed from app.py socketio = None @@ -131,7 +147,8 @@ def update_directory(): def get_series(): """Get all series data.""" try: - if series_app is None or series_app.List is None: + current_series_app = get_series_app() + if current_series_app is None or current_series_app.List is None: return jsonify({ 'status': 'success', 'series': [], @@ -141,7 +158,7 @@ def get_series(): # Get series data series_data = [] - for serie in series_app.List.GetList(): + for serie in current_series_app.List.GetList(): series_data.append({ 'folder': serie.folder, 'name': serie.name or serie.folder, @@ -192,14 +209,15 @@ def search_series(): }), 400 # Check if series_app is available - if series_app is None: + current_series_app = get_series_app() + if current_series_app is None: return jsonify({ 'status': 'error', 'message': 'Series application not initialized' }), 500 # Perform the search - search_results = series_app.search(query) + search_results = current_series_app.search(query) # Format results for the frontend results = [] @@ -819,9 +837,10 @@ def process_locks_status(): } }) -# Initialize the series app when the blueprint is loaded -try: - init_series_app() -except Exception as e: - print(f"Failed to initialize series app in API blueprint: {e}") - series_app = None \ No newline at end of file +# Initialize the series app when needed (now handled in main app.py) +# Commenting out module-level initialization to prevent duplicate initialization +# try: +# init_series_app() +# except Exception as e: +# print(f"Failed to initialize series app in API blueprint: {e}") +# series_app = None \ No newline at end of file