Fix double SeriesApp initialization on startup

- Moved sync_series_from_data_files() before DownloadService init
- Ensures series are in DB before main SeriesApp creation
- Eliminates redundant SeriesApp instantiation during startup
- Updated README to clarify initialization sequence
This commit is contained in:
2026-01-18 15:49:58 +01:00
parent 7a77dff194
commit ea9e959a7b
2 changed files with 15 additions and 14 deletions

View File

@@ -159,11 +159,12 @@ On first startup, the application performs a one-time sync of series from data f
1. FastAPI lifespan starts 1. FastAPI lifespan starts
2. Database is initialized 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 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 ### Adding New Series

View File

@@ -128,36 +128,36 @@ async def lifespan(_application: FastAPI):
# Subscribe to progress events # Subscribe to progress events
progress_service.subscribe("progress_updated", progress_event_handler) progress_service.subscribe("progress_updated", progress_event_handler)
# Initialize download service and restore queue from database # Sync series from data files to database FIRST (one-time setup)
# Only if anime directory is configured # This must happen before SeriesApp initialization
try: try:
from src.server.utils.dependencies import get_download_service
logger.info( logger.info(
"Checking anime_directory setting: '%s'", "Checking anime_directory setting: '%s'",
settings.anime_directory settings.anime_directory
) )
if settings.anime_directory: if settings.anime_directory:
download_service = get_download_service() # Sync series from data files to database (one-time setup)
await download_service.initialize()
logger.info("Download service initialized and queue restored")
# Sync series from data files to database
sync_count = await sync_series_from_data_files( sync_count = await sync_series_from_data_files(
settings.anime_directory settings.anime_directory
) )
logger.info( logger.info(
"Data file sync complete. Added %d series.", sync_count "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: else:
logger.info( logger.info(
"Download service initialization skipped - " "Download service initialization skipped - "
"anime directory not configured" "anime directory not configured"
) )
except (OSError, RuntimeError, ValueError) as e: except (OSError, RuntimeError, ValueError) as e:
logger.warning("Failed to initialize download service: %s", e) logger.warning("Failed to initialize services: %s", e)
# Continue startup - download service can be initialized later # Continue startup - services can be initialized later
logger.info("FastAPI application started successfully") logger.info("FastAPI application started successfully")
logger.info("Server running on http://127.0.0.1:8000") logger.info("Server running on http://127.0.0.1:8000")