Fix: Use yt_dlp.utils.DownloadCancelled for proper download cancellation
- Import and use DownloadCancelled exception which YT-DLP properly handles - Add InterruptedError handling throughout the call chain - Fire 'cancelled' status event when download is cancelled - Handle InterruptedError in DownloadService to set CANCELLED status
This commit is contained in:
@@ -846,6 +846,7 @@ class AnimeService:
|
||||
|
||||
Raises:
|
||||
AnimeServiceError: If download fails
|
||||
InterruptedError: If download was cancelled
|
||||
|
||||
Note:
|
||||
The 'key' parameter is the primary identifier used for all
|
||||
@@ -864,6 +865,10 @@ class AnimeService:
|
||||
key=key,
|
||||
item_id=item_id,
|
||||
)
|
||||
except InterruptedError:
|
||||
# Download was cancelled - re-raise for proper handling
|
||||
logger.info("Download cancelled, propagating cancellation")
|
||||
raise
|
||||
except Exception as exc:
|
||||
logger.exception("download failed")
|
||||
raise AnimeServiceError("Download failed") from exc
|
||||
|
||||
@@ -952,7 +952,7 @@ class DownloadService:
|
||||
except asyncio.CancelledError:
|
||||
# Handle task cancellation during shutdown
|
||||
logger.info(
|
||||
"Download cancelled during shutdown: item_id=%s",
|
||||
"Download task cancelled: item_id=%s",
|
||||
item.id,
|
||||
)
|
||||
item.status = DownloadStatus.CANCELLED
|
||||
@@ -965,6 +965,23 @@ class DownloadService:
|
||||
# Re-save to database as pending
|
||||
await self._save_to_database(item)
|
||||
raise # Re-raise to properly cancel the task
|
||||
|
||||
except InterruptedError:
|
||||
# Handle download cancellation from provider
|
||||
logger.info(
|
||||
"Download interrupted/cancelled: item_id=%s",
|
||||
item.id,
|
||||
)
|
||||
item.status = DownloadStatus.CANCELLED
|
||||
item.completed_at = datetime.now(timezone.utc)
|
||||
# Delete cancelled item from database
|
||||
await self._delete_from_database(item.id)
|
||||
# Return item to pending queue if not shutting down
|
||||
if not self._is_shutting_down:
|
||||
self._add_to_pending_queue(item, front=True)
|
||||
# Re-save to database as pending
|
||||
await self._save_to_database(item)
|
||||
# Don't re-raise - this is handled gracefully
|
||||
|
||||
except Exception as e:
|
||||
# Handle failure
|
||||
|
||||
Reference in New Issue
Block a user