feat(dl): name the free space of a nearly full temp volume on the failure line

This commit is contained in:
imSp4rky
2026-11-28 14:35:29 -07:00
parent 5167f4f9d9
commit 7856d53950
8 changed files with 156 additions and 9 deletions
+35 -2
View File
@@ -21,6 +21,7 @@ import mediaexport
from click.core import ParameterSource
from langcodes import Language
from pymediainfo import MediaInfo
from rich import filesize
from rich.console import Group
from rich.padding import Padding
from rich.rule import Rule
@@ -74,7 +75,7 @@ from unshackle.core.remote_protocol import APIErrorCode, coded
from unshackle.core.run_plan import PlanError, RunOutcome, RunStopped, bitrate_range, check_options
from unshackle.core.service import grow_session_pool
from unshackle.core.services import ROUTING_PARAMS, Services
from unshackle.core.temp import with_task_temp
from unshackle.core.temp import temp_root, with_task_temp
from unshackle.core.title_metadata import Enrichment, TitleMetadata, apply_enrich
from unshackle.core.title_source import KeyArrival, TitleSource
from unshackle.core.titles import Movie, Movies, Series, Song, Title_T
@@ -370,6 +371,28 @@ def download_tracks_in_passes(
DOWNLOAD_CANCELLED.clear() # never leave a failed track's cancel set for later code
LOW_FREE_SPACE = 1024**3
"""Free bytes on the temp volume below which a failure line names them."""
def low_free_space(e: BaseException) -> str:
"""``", 38.0 MB free on <temp>"`` when the temp volume is nearly full at ``e``'s first report, else empty.
The reading is kept on ``e``: a failed track deletes its files before the run reports it, and the
API job reports it again after the task temp dir is gone, so the first reading is the true one.
"""
if (cached := getattr(e, "free_space", None)) is not None:
return str(cached)
temp = temp_root()
try:
free = shutil.disk_usage(temp).free
except OSError:
free = LOW_FREE_SPACE
note = f", {filesize.decimal(free)} free on {temp}" if free < LOW_FREE_SPACE else ""
e.free_space = note # type: ignore[attr-defined]
return note
class dl:
def find_custom_font(self, font_name: str) -> Path | None:
"""
@@ -2913,6 +2936,8 @@ class dl:
line = f"{type(e).__name__}: {e}"
if retries_exhausted(e):
line += f", {'; '.join(e.__notes__)}"
if free := low_free_space(e):
line = line.removesuffix(".") + free
return [":x: Download Failed...", f" {line}"]
def start_key_resolution(
@@ -2931,7 +2956,15 @@ class dl:
export=partial(self.write_export, export) if export else None,
debug_logger=self.debug_logger,
)
verify.DECRYPT_HOOK = self.keys.decrypt_verified
verify.DECRYPT_HOOK = self.decrypt_verified
def decrypt_verified(self, *args: Any, **kwargs: Any) -> None:
"""The run's verified decrypt, which reads the free space of a failed decrypt before its track deletes its files."""
try:
self.keys.decrypt_verified(*args, **kwargs)
except Exception as e:
low_free_space(e)
raise
def cdm_for(self, system: str, quality: int | None = None) -> object | None:
"""Get the CDM for one DRM system, ``widevine`` or ``playready``.
+3
View File
@@ -33,6 +33,7 @@ from rich import filesize
from unshackle.core.config import config
from unshackle.core.constants import DOWNLOAD_CANCELLED, DownloadCancelled
from unshackle.core.remote_protocol import APIErrorCode, coded
from unshackle.core.utilities import get_debug_logger, get_extension, log_event
from unshackle.core.utils import run_proxy
@@ -1205,6 +1206,8 @@ def download(
# so the caller fails fast instead of hitting a missing segment at merge time
if not cancelled:
exc.add_note(f"{GAVE_UP_NOTE} {failures} failed attempts over {_time() - started:.0f}s")
if disk_error:
coded(exc, APIErrorCode.DOWNLOAD_ERROR)
raise
return
if not part_mode:
+8 -4
View File
@@ -70,15 +70,19 @@ def sweep_task_dirs(root: Path) -> None:
entry.unlink(missing_ok=True)
def temp_root() -> Path:
"""The configured temp dir, also when config.directories.temp points at a task dir inside it."""
temp = config.directories.temp
return temp.parent if temp.name.startswith(TASK_PREFIX) else temp
def resume_stash() -> Path | None:
"""The dir beside the task dirs that keeps a failed track's segments for the next run.
None outside a task dir: there config.directories.temp itself outlives the run.
"""
temp = config.directories.temp
if not temp.name.startswith(TASK_PREFIX):
return None
return temp.parent / RESUME_STASH
root = temp_root()
return None if root == config.directories.temp else root / RESUME_STASH
def _move_with_sidecar(source: Path, target: Path) -> bool: