diff --git a/shelfmark/download/clients/__init__.py b/shelfmark/download/clients/__init__.py index 1214c458..0d221279 100644 --- a/shelfmark/download/clients/__init__.py +++ b/shelfmark/download/clients/__init__.py @@ -49,6 +49,8 @@ def with_retry( base_delay: float = 1.0, max_delay: float = 10.0, jitter: float = 0.5, + *, + extra_exceptions: tuple[type[Exception], ...] = (), ) -> Callable[[Callable[..., T]], Callable[..., T]]: """Retry API calls with exponential backoff. @@ -57,6 +59,7 @@ def with_retry( base_delay: Initial delay in seconds (default 1.0) max_delay: Maximum delay cap in seconds (default 10.0) jitter: Random jitter factor 0-1 to add to delay (default 0.5) + extra_exceptions: Client-library exceptions that also allow a retry Retries on: - Connection errors @@ -69,6 +72,8 @@ def with_retry( """ + retryable_exceptions = RETRYABLE_EXCEPTIONS + extra_exceptions + def decorator(func: Callable[..., T]) -> Callable[..., T]: @wraps(func) def wrapper(*args: object, **kwargs: object) -> T: @@ -82,7 +87,7 @@ def with_retry( if e.response is not None and e.response.status_code < _MIN_RETRYABLE_STATUS: raise last_exception = e - except RETRYABLE_EXCEPTIONS as e: + except retryable_exceptions as e: last_exception = e if attempt < max_attempts: diff --git a/shelfmark/download/clients/transmission.py b/shelfmark/download/clients/transmission.py index e4d57d43..573a8567 100644 --- a/shelfmark/download/clients/transmission.py +++ b/shelfmark/download/clients/transmission.py @@ -9,12 +9,15 @@ import importlib from contextlib import contextmanager, suppress from typing import TYPE_CHECKING, Protocol, TypeGuard +from transmission_rpc.error import TransmissionConnectError + from shelfmark.core.config import config from shelfmark.core.logger import setup_logger from shelfmark.download.clients import ( DownloadClient, DownloadStatus, register_client, + with_retry, ) from shelfmark.download.clients._coercion import ( coerce_optional_float, @@ -37,6 +40,8 @@ except ImportError: if TYPE_CHECKING: from collections.abc import Iterator + from transmission_rpc import Torrent + logger = setup_logger(__name__) _SEEDING_PROGRESS_PERCENT = 100 @@ -294,6 +299,11 @@ class TransmissionClient(DownloadClient): else: return torrent_hash + @with_retry(extra_exceptions=(TransmissionConnectError,)) + def _get_torrent(self, download_id: str) -> Torrent: + """Allow transient RPC outages to recover before reporting a failed download.""" + return self._client.get_torrent(download_id) + def get_status(self, download_id: str) -> DownloadStatus: """Get torrent status by hash. @@ -305,7 +315,7 @@ class TransmissionClient(DownloadClient): """ try: - torrent = self._client.get_torrent(download_id) + torrent = self._get_torrent(download_id) # Transmission status values: # 0: stopped