soulsync/core/downloads/cancel.py
Broque Thomas dc2835eecc PR4b: lift cancel + clear download routes to core/downloads/cancel.py
Second sub-PR in the download orchestrator series. Strict 1:1 lift —
zero behavior change.

What moved:
- cancel_download (single slskd cancel) → cancel_single_download
- cancel_all_downloads (cancel + clear + sweep) → cancel_all_active
- clear_finished_downloads (slskd clear + sweep) → clear_finished_active
- clear_completed_downloads (local task tracker prune) → clear_completed_local

Slskd-touching helpers take (soulseek_client, run_async, sweep_callback)
explicitly so the route layer wires the live client + the existing
_sweep_empty_download_directories helper. The local-state helper imports
download_tasks/download_batches/batch_locks/tasks_lock straight from
core.runtime_state since those are module-level shared globals.

Prep change: `batch_locks` dict moved from web_server.py global into
core/runtime_state.py alongside the other download globals. web_server.py
re-imports from runtime_state so the ~3 existing call sites in
web_server.py keep resolving without modification. Identity preserved
(same dict across all importers).

Out of scope (deferred to PR4g batch lifecycle):
- cancel_download_task (calls _on_download_completed)
- cancel_task_v2 + _atomic_cancel_task + _find_task_by_playlist_track
  (manipulate batch active_count directly, deeply coupled to lifecycle)

Behavior parity:
- Same response shapes + status codes on each route
- Same call order (cancel_all → clear_all_completed → sweep)
- Same conditional sweep on clear_finished (skipped on failure)
- Same sweep ALWAYS runs after cancel_all even if clear_all returns False
  (matches original — clear failure was non-fatal in cancel_all path)
- Same TERMINAL_STATUSES set: completed/failed/not_found/cancelled/skipped/
  already_owned (lifted to module-level constant)
- Same empty-batch pruning + same batch_locks cleanup
- Same lock acquisition pattern (single tasks_lock)

Tests: 14 new under tests/downloads/test_downloads_cancel.py covering
single cancel, cancel-all happy + failure paths, clear-finished + sweep
gate, local task pruning across all 7 active/terminal states, batch
queue trimming, batch_locks cleanup.

Full suite: 921 passing (was 907). Ruff clean.
2026-04-27 21:41:35 -07:00

103 lines
3.6 KiB
Python

"""Download cancellation + clear helpers.
Four discrete operations lifted from web_server.py:
- `cancel_single_download(client, run_async, download_id, username)` — cancel
one slskd transfer.
- `cancel_all_active(client, run_async, sweep_callback)` — cancel every
active slskd transfer, then clear the now-cancelled ones, then sweep
empty download directories.
- `clear_finished_active(client, run_async, sweep_callback)` — clear all
terminal transfers from slskd (no cancel step), sweep dirs.
- `clear_completed_local()` — prune terminal-status tasks from the
local `download_tasks` tracker, drop empty batches, drop their locks.
Pure local mutation, doesn't touch slskd.
The slskd-touching helpers take the soulseek client and run_async callback
explicitly; the local helper imports its globals directly from
`core.runtime_state` since those are module-level shared state and every
caller sees the same dict.
Out of scope for this PR (deferred to the batch-lifecycle lift):
- `cancel_download_task` (calls _on_download_completed)
- `cancel_task_v2` + `_atomic_cancel_task` (manipulate batch active_count)
"""
from __future__ import annotations
import logging
from typing import Callable
from core.runtime_state import (
batch_locks,
download_batches,
download_tasks,
tasks_lock,
)
logger = logging.getLogger(__name__)
_TERMINAL_STATUSES = {
'completed', 'failed', 'not_found', 'cancelled', 'skipped', 'already_owned',
}
def cancel_single_download(soulseek_client, run_async: Callable,
download_id: str, username: str) -> bool:
"""Cancel one specific slskd download (with `remove=True`)."""
return run_async(soulseek_client.cancel_download(download_id, username, remove=True))
def cancel_all_active(soulseek_client, run_async: Callable,
sweep_callback: Callable[[], None]) -> tuple[bool, str]:
"""Cancel every active slskd download, clear the resulting ones, sweep dirs.
Returns `(success, message)` so the route can map to the right HTTP shape.
"""
cancel_success = run_async(soulseek_client.cancel_all_downloads())
if not cancel_success:
return False, "Failed to cancel active downloads."
run_async(soulseek_client.clear_all_completed_downloads())
sweep_callback()
return True, "All downloads cancelled and cleared."
def clear_finished_active(soulseek_client, run_async: Callable,
sweep_callback: Callable[[], None]) -> bool:
"""Clear all terminal transfers from slskd, sweep dirs on success."""
success = run_async(soulseek_client.clear_all_completed_downloads())
if success:
sweep_callback()
return success
def clear_completed_local() -> int:
"""Remove completed/failed/cancelled tasks from the local tracker.
Also prunes batches whose queues are now empty, and removes the matching
`batch_locks` entry. Returns the number of cleared tasks.
"""
cleared = 0
with tasks_lock:
task_ids_to_remove = [
tid for tid, task in download_tasks.items()
if task.get('status') in _TERMINAL_STATUSES
]
for tid in task_ids_to_remove:
del download_tasks[tid]
cleared += 1
empty_batches = []
for bid, batch in download_batches.items():
remaining = [t for t in batch.get('queue', []) if t in download_tasks]
if not remaining:
empty_batches.append(bid)
else:
batch['queue'] = remaining
for bid in empty_batches:
del download_batches[bid]
if bid in batch_locks:
del batch_locks[bid]
return cleared