fix queue error

This commit is contained in:
2025-12-10 20:55:09 +01:00
parent 798461a1ea
commit 99f79e4c29
6 changed files with 263 additions and 683 deletions

View File

@@ -25,8 +25,11 @@ from src.server.services.download_service import DownloadService, DownloadServic
class MockQueueRepository:
"""Mock implementation of QueueRepository for testing.
This provides an in-memory storage that mimics the database repository
behavior without requiring actual database connections.
This provides an in-memory storage that mimics the simplified database
repository behavior without requiring actual database connections.
Note: The repository is simplified - status, priority, progress are
now managed in-memory by DownloadService, not stored in database.
"""
def __init__(self):
@@ -42,81 +45,19 @@ class MockQueueRepository:
"""Get item by ID from in-memory storage."""
return self._items.get(item_id)
async def get_pending_items(self) -> List[DownloadItem]:
"""Get all pending items."""
return [
item for item in self._items.values()
if item.status == DownloadStatus.PENDING
]
async def get_all_items(self) -> List[DownloadItem]:
"""Get all items in storage."""
return list(self._items.values())
async def get_active_item(self) -> Optional[DownloadItem]:
"""Get the currently active item."""
for item in self._items.values():
if item.status == DownloadStatus.DOWNLOADING:
return item
return None
async def get_completed_items(
self, limit: int = 100
) -> List[DownloadItem]:
"""Get completed items."""
completed = [
item for item in self._items.values()
if item.status == DownloadStatus.COMPLETED
]
return completed[:limit]
async def get_failed_items(self, limit: int = 50) -> List[DownloadItem]:
"""Get failed items."""
failed = [
item for item in self._items.values()
if item.status == DownloadStatus.FAILED
]
return failed[:limit]
async def update_status(
async def set_error(
self,
item_id: str,
status: DownloadStatus,
error: Optional[str] = None
error: str,
) -> bool:
"""Update item status."""
"""Set error message on an item."""
if item_id not in self._items:
return False
self._items[item_id].status = status
if error:
self._items[item_id].error = error
if status == DownloadStatus.COMPLETED:
self._items[item_id].completed_at = datetime.now(timezone.utc)
elif status == DownloadStatus.DOWNLOADING:
self._items[item_id].started_at = datetime.now(timezone.utc)
return True
async def update_progress(
self,
item_id: str,
progress: float,
downloaded: int,
total: int,
speed: float
) -> bool:
"""Update download progress."""
if item_id not in self._items:
return False
item = self._items[item_id]
if item.progress is None:
from src.server.models.download import DownloadProgress
item.progress = DownloadProgress(
percent=progress,
downloaded_bytes=downloaded,
total_bytes=total,
speed_bps=speed
)
else:
item.progress.percent = progress
item.progress.downloaded_bytes = downloaded
item.progress.total_bytes = total
item.progress.speed_bps = speed
self._items[item_id].error = error
return True
async def delete_item(self, item_id: str) -> bool:
@@ -126,15 +67,11 @@ class MockQueueRepository:
return True
return False
async def clear_completed(self) -> int:
"""Clear all completed items."""
completed_ids = [
item_id for item_id, item in self._items.items()
if item.status == DownloadStatus.COMPLETED
]
for item_id in completed_ids:
del self._items[item_id]
return len(completed_ids)
async def clear_all(self) -> int:
"""Clear all items."""
count = len(self._items)
self._items.clear()
return count
@pytest.fixture
@@ -505,9 +442,9 @@ class TestPersistence:
)
# Item should be saved in mock repository
pending_items = await mock_queue_repository.get_pending_items()
assert len(pending_items) == 1
assert pending_items[0].serie_id == "series-1"
all_items = await mock_queue_repository.get_all_items()
assert len(all_items) == 1
assert all_items[0].serie_id == "series-1"
@pytest.mark.asyncio
async def test_queue_recovery_after_restart(