refactor: centralize initialization logic in dedicated service
- Create initialization_service.py with shared initialization functions - Extract setup logic from lifespan and setup endpoint into reusable functions - Setup endpoint now calls perform_initial_setup() directly - Lifespan startup calls the same shared functions - Eliminates code duplication between setup and lifespan - Ensures consistent initialization behavior regardless of entry point
This commit is contained in:
@@ -117,46 +117,26 @@ async def setup_auth(req: SetupRequest):
|
||||
# Save the config with all updates
|
||||
config_service.save_config(config, create_backup=False)
|
||||
|
||||
# Sync series from data files to database if anime directory is set
|
||||
if anime_directory:
|
||||
try:
|
||||
import structlog
|
||||
|
||||
from src.server.database.connection import get_db_session
|
||||
from src.server.database.system_settings_service import (
|
||||
SystemSettingsService,
|
||||
)
|
||||
from src.server.services.anime_service import (
|
||||
sync_series_from_data_files,
|
||||
)
|
||||
logger = structlog.get_logger(__name__)
|
||||
sync_count = await sync_series_from_data_files(
|
||||
anime_directory, logger
|
||||
)
|
||||
logger.info(
|
||||
"Setup complete: synced series from data files",
|
||||
count=sync_count
|
||||
)
|
||||
|
||||
# Mark initial scan as completed
|
||||
try:
|
||||
async with get_db_session() as db:
|
||||
await SystemSettingsService.mark_initial_scan_completed(
|
||||
db
|
||||
)
|
||||
logger.info("Marked initial scan as completed")
|
||||
except Exception as mark_error:
|
||||
logger.warning(
|
||||
"Failed to mark initial scan as completed",
|
||||
error=str(mark_error)
|
||||
)
|
||||
except Exception as e:
|
||||
# Log but don't fail setup if sync fails
|
||||
import structlog
|
||||
structlog.get_logger(__name__).warning(
|
||||
"Failed to sync series after setup",
|
||||
error=str(e)
|
||||
)
|
||||
# Perform initial setup (series sync, NFO scan, media scan)
|
||||
# This ensures everything is initialized immediately after setup
|
||||
# without requiring an application restart
|
||||
# Reload settings to pick up the new configuration
|
||||
from src.config.settings import settings
|
||||
from src.server.services.initialization_service import (
|
||||
perform_initial_setup,
|
||||
perform_nfo_scan_if_needed,
|
||||
)
|
||||
settings.reload()
|
||||
|
||||
# Perform the initial series sync and mark as completed
|
||||
await perform_initial_setup()
|
||||
|
||||
# Perform NFO scan if configured
|
||||
await perform_nfo_scan_if_needed()
|
||||
|
||||
# Note: Media scan is skipped during setup as it requires
|
||||
# background_loader service which is only available during
|
||||
# application lifespan. It will run on first application startup.
|
||||
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user