feat(tracks): resume segment decryption and rolling merge through the segment store

This commit is contained in:
imSp4rky
2026-11-28 22:09:44 -07:00
parent f4d178c26b
commit 82991dd823
26 changed files with 1125 additions and 186 deletions
+112 -1
View File
@@ -8,12 +8,14 @@ import shutil
import time
from functools import partial
from pathlib import Path
from types import SimpleNamespace
import pytest
from filelock import FileLock
from unshackle.core.config import config
from unshackle.core.constants import DOWNLOAD_CANCELLED, DOWNLOAD_LICENCE_ONLY, DownloadCancelled
from unshackle.core.temp import RESUME_STASH, STASH_MAX_AGE, resume_stash, sweep_task_dirs, task_temp_dir
from unshackle.core.temp import LOCK_NAME, RESUME_STASH, STASH_MAX_AGE, resume_stash, sweep_task_dirs, task_temp_dir
from unshackle.core.tracks import Video
from unshackle.core.tracks.segment_store import DownloadMode, SegmentStore
@@ -112,6 +114,76 @@ def test_failed_track_resumes_in_next_run(temp_root, monkeypatch):
assert not stashed.exists()
def killed_run(temp_root: Path, name: str = "task_killed", segments: str = SEG_NAME) -> Path:
"""The task dir of a run killed mid-download: its lock file, a sidecar, a complete and a partial segment."""
task_dir = temp_root / name
task_dir.mkdir()
(task_dir / LOCK_NAME).touch()
save_dir = task_dir / segments
seed(SimpleNamespace(save_dir=save_dir))
(save_dir / "1.mp4.p.!dev").write_bytes(b"partial")
return task_dir
def test_a_killed_run_resumes_from_its_task_dir(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", True)
killed = killed_run(temp_root)
seen = {}
def second_parser(track, ctx):
seen["files"] = sorted(path.name for path in ctx.save_dir.iterdir())
seen["reusable"] = open_store(ctx.save_dir)
ctx.save_path.write_bytes(b"output")
track.path = ctx.save_path
with task_temp_dir():
run_download(monkeypatch, second_parser)
assert seen == {"files": ["0.mp4"], "reusable": True}
assert not (killed / SEG_NAME / "0.mp4").exists()
def test_the_sweep_stashes_what_a_killed_run_keeps_and_removes_the_rest(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", True)
killed = killed_run(temp_root)
(killed / "Audio_t2.mp4").write_bytes(b"a finished track")
old = killed_run(temp_root, "task_old", "Video_old.mp4_segments")
when = time.time() - STASH_MAX_AGE - 60
for save_dir in (killed / SEG_NAME, old / "Video_old.mp4_segments"):
for path in (save_dir.parent, save_dir, store(save_dir).sidecar):
os.utime(path, (when, when))
sweep_task_dirs(temp_root)
stashed = temp_root / RESUME_STASH / SEG_NAME
assert (stashed / "0.mp4").read_bytes() == b"complete segment"
assert store(stashed).sidecar.exists()
assert not killed.exists()
assert not old.exists()
# the stash ages an entry from when it arrived there, not from when the killed run wrote it
assert len(list((temp_root / RESUME_STASH).iterdir())) == 4
def test_a_killed_run_resumes_after_the_sweep_probed_its_lock(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", True)
killed = killed_run(temp_root)
when = time.time() - 120
os.utime(killed, (when, when))
with task_temp_dir() as task_dir:
store(task_dir / SEG_NAME).restore()
assert (task_dir / SEG_NAME / "0.mp4").read_bytes() == b"complete segment"
def test_a_running_task_keeps_its_segments(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", True)
running = killed_run(temp_root)
with FileLock(running / LOCK_NAME), task_temp_dir() as task_dir:
store(task_dir / SEG_NAME).restore()
assert not (task_dir / SEG_NAME).exists()
assert (running / SEG_NAME / "0.mp4").exists()
def test_flag_off_stashes_nothing(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", False)
@@ -133,6 +205,45 @@ def test_partials_are_not_stashed(temp_root, monkeypatch):
assert not list((temp_root / RESUME_STASH).rglob("*.!dev"))
def test_a_checkpointed_output_is_stashed_beside_its_segments(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", True)
with task_temp_dir() as task_dir:
save_dir = task_dir / SEG_NAME
rolling = store(save_dir)
rolling.open(make_track().url, SEGMENTS, mode=DownloadMode(rolling_merge=True))
(save_dir / "1.mp4").write_bytes(b"next segment")
(save_dir / "2.mp4").write_bytes(b"written")
(save_dir / "2.mp4.!dev").write_bytes(b"control")
(task_dir / "Video_t1.mp4").write_bytes(b"init+0")
assert rolling.checkpoint(1, 6)
rolling.abandon()
stashed = temp_root / RESUME_STASH / SEG_NAME
assert sorted(path.name for path in stashed.iterdir()) == ["1.mp4"]
assert (temp_root / RESUME_STASH / "Video_t1.mp4").read_bytes() == b"init+0"
@pytest.mark.skipif(os.name == "nt", reason="a symlink needs a privilege on Windows")
def test_a_symlinked_output_is_never_stashed(temp_root, monkeypatch, tmp_path):
monkeypatch.setattr(config, "continue_downloads", True)
elsewhere = tmp_path / "elsewhere.mp4"
elsewhere.write_bytes(b"not ours")
with task_temp_dir() as task_dir:
save_dir = task_dir / SEG_NAME
rolling = store(save_dir)
rolling.open(make_track().url, SEGMENTS, mode=DownloadMode(rolling_merge=True))
(save_dir / "1.mp4").write_bytes(b"next segment")
(task_dir / "Video_t1.mp4").symlink_to(elsewhere)
rolling._record["merge"] = [1, 6]
rolling._write()
rolling.abandon()
assert not (temp_root / RESUME_STASH / "Video_t1.mp4").exists()
assert elsewhere.read_bytes() == b"not ours"
def test_existing_stash_target_is_left_alone(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", True)
with task_temp_dir() as task_dir: