diff --git a/README.md b/README.md index b39dac8..da316b6 100644 --- a/README.md +++ b/README.md @@ -159,11 +159,12 @@ On first startup, the application performs a one-time sync of series from data f 1. FastAPI lifespan starts 2. Database is initialized -3. `sync_series_from_data_files()` reads all data files from the anime directory +3. `sync_series_from_data_files()` reads all data files from the anime directory (creates temporary SeriesApp) 4. Series metadata is synced to the database -5. `SeriesApp` loads series from database (not from files) +5. DownloadService initializes (triggers main `SeriesApp` creation) +6. `SeriesApp` loads series from database via service layer (not from files) -On subsequent startups, `SeriesApp` initializes with an empty series list (`skip_load=True`). Series are loaded from the database by the service layer as needed, avoiding redundant file system scans. +On subsequent startups, the same flow applies but the sync finds no new series. `SeriesApp` always initializes with an empty series list (`skip_load=True`) and loads data from the database on demand, avoiding redundant file system scans. ### Adding New Series diff --git a/src/server/fastapi_app.py b/src/server/fastapi_app.py index a7a44ec..808ce48 100644 --- a/src/server/fastapi_app.py +++ b/src/server/fastapi_app.py @@ -128,36 +128,36 @@ async def lifespan(_application: FastAPI): # Subscribe to progress events progress_service.subscribe("progress_updated", progress_event_handler) - # Initialize download service and restore queue from database - # Only if anime directory is configured + # Sync series from data files to database FIRST (one-time setup) + # This must happen before SeriesApp initialization try: - from src.server.utils.dependencies import get_download_service - logger.info( "Checking anime_directory setting: '%s'", settings.anime_directory ) if settings.anime_directory: - download_service = get_download_service() - await download_service.initialize() - logger.info("Download service initialized and queue restored") - - # Sync series from data files to database + # Sync series from data files to database (one-time setup) sync_count = await sync_series_from_data_files( settings.anime_directory ) logger.info( "Data file sync complete. Added %d series.", sync_count ) + + # Now initialize download service (will use data from database) + from src.server.utils.dependencies import get_download_service + download_service = get_download_service() + await download_service.initialize() + logger.info("Download service initialized and queue restored") else: logger.info( "Download service initialization skipped - " "anime directory not configured" ) except (OSError, RuntimeError, ValueError) as e: - logger.warning("Failed to initialize download service: %s", e) - # Continue startup - download service can be initialized later + logger.warning("Failed to initialize services: %s", e) + # Continue startup - services can be initialized later logger.info("FastAPI application started successfully") logger.info("Server running on http://127.0.0.1:8000")