fix: add and download issue

This commit is contained in:
Lukas 2025-11-14 09:33:36 +01:00
parent 2441730862
commit 1e357181b6
2 changed files with 25 additions and 8 deletions

View File

@ -85,19 +85,26 @@ class DownloadService:
self._total_downloaded_mb: float = 0.0
self._download_speeds: deque[float] = deque(maxlen=10)
# Track if queue progress has been initialized
self._queue_progress_initialized: bool = False
# Load persisted queue
self._load_queue()
# Initialize queue progress tracking
asyncio.create_task(self._init_queue_progress())
logger.info(
"DownloadService initialized",
max_retries=max_retries,
)
async def _init_queue_progress(self) -> None:
"""Initialize the download queue progress tracking."""
"""Initialize the download queue progress tracking.
This is called lazily when queue processing starts to ensure
the event loop is running and the coroutine can be properly awaited.
"""
if self._queue_progress_initialized:
return
try:
from src.server.services.progress_service import ProgressType
await self._progress_service.start_progress(
@ -106,6 +113,7 @@ class DownloadService:
title="Download Queue",
message="Queue ready",
)
self._queue_progress_initialized = True
except Exception as e:
logger.error("Failed to initialize queue progress", error=str(e))
@ -239,6 +247,9 @@ class DownloadService:
Raises:
DownloadServiceError: If adding items fails
"""
# Initialize queue progress tracking if not already done
await self._init_queue_progress()
created_ids = []
try:
@ -363,6 +374,9 @@ class DownloadService:
DownloadServiceError: If queue processing is already active
"""
try:
# Initialize queue progress tracking if not already done
await self._init_queue_progress()
# Check if download already active
if self._active_download:
raise DownloadServiceError(

View File

@ -157,7 +157,7 @@ class ProgressService:
self._event_handlers[event_name] = []
self._event_handlers[event_name].append(handler)
logger.debug("Event handler subscribed", event=event_name)
logger.debug("Event handler subscribed", event_type=event_name)
def unsubscribe(
self, event_name: str, handler: Callable[[ProgressEvent], None]
@ -171,10 +171,13 @@ class ProgressService:
if event_name in self._event_handlers:
try:
self._event_handlers[event_name].remove(handler)
logger.debug("Event handler unsubscribed", event=event_name)
logger.debug(
"Event handler unsubscribed", event_type=event_name
)
except ValueError:
logger.warning(
"Handler not found for unsubscribe", event=event_name
"Handler not found for unsubscribe",
event_type=event_name,
)
async def _emit_event(self, event: ProgressEvent) -> None:
@ -204,7 +207,7 @@ class ProgressService:
if isinstance(result, Exception):
logger.error(
"Event handler raised exception",
event=event_name,
event_type=event_name,
error=str(result),
handler_index=idx,
)