feat(orchestrator): add ThreadPoolExecutor parallel download engine (#16)
Replace the sequential for-loop in cli.py with download_all_books() which submits all books as futures to a ThreadPoolExecutor. Each future runs the full download pipeline (API → streaming → history) independently; failures are isolated per book and siblings continue unaffected. OrchestratorResult dataclass returns stable-sorted counts and per-book details (failed_books as (Book, reason) tuples, skipped_books as Book objects). The _raw_to_book() conversion is extracted from cli.py so the orchestrator owns the dict→Book mapping. All 20 unit tests use an injected download_fn — no network or filesystem required. Co-Authored-By: pi <pi@earendil.works>
This commit is contained in:
1 parent
6418c63c2f
commit
e7e6c625b1
6 files changed
+894
-74
No files matched your search
+24
-50
@@ -9,6 +9,7 @@ from librofm_downloader.config import load_config, ConfigError
|
||||
from librofm_downloader.client import LibroFmClient, AuthError
|
||||
from librofm_downloader.history import DownloadHistory
|
||||
from librofm_downloader.downloader import Book, download_book
|
||||
from librofm_downloader.orchestrator import download_all_books
|
||||
from librofm_downloader.progress import DownloadReporter
|
||||
|
||||
console = Console()
|
||||
@@ -115,41 +116,16 @@ def run(
|
||||
reporter = DownloadReporter()
|
||||
console.print(f"\n[bold]{len(new_books)} book(s) to download:[/bold]\n")
|
||||
|
||||
downloaded = 0
|
||||
skipped = 0
|
||||
failed = 0
|
||||
failed_books: list[tuple[Book, str]] = []
|
||||
skipped_books: list[Book] = []
|
||||
# --- Download loop (parallel via orchestrator — Issue #16) ---
|
||||
|
||||
for raw_book in new_books:
|
||||
title = raw_book.get("title", "Unknown")
|
||||
isbn = raw_book.get("isbn", "?")
|
||||
authors = raw_book.get("authors", [])
|
||||
audiobook_info = raw_book.get("audiobook_info", {}) or {}
|
||||
narrators = audiobook_info.get("narrators", []) or raw_book.get("narrators", [])
|
||||
|
||||
if verbose:
|
||||
console.print(f" ⬇ {title} [dim]({isbn})[/dim]")
|
||||
console.print(f" authors: {', '.join(authors) or '?'}")
|
||||
console.print(f" narrators: {', '.join(narrators) or '?'}")
|
||||
|
||||
book = Book(
|
||||
title=title,
|
||||
authors=authors,
|
||||
narrators=narrators,
|
||||
isbn=isbn,
|
||||
series=raw_book.get("series", ""),
|
||||
series_num=raw_book.get("series_num"),
|
||||
cover_url=raw_book.get("cover_url", ""),
|
||||
pdf_extras=bool(audiobook_info.get("pdf_extras")) if audiobook_info else False,
|
||||
publication_year=raw_book.get("publication_year"),
|
||||
publication_month=raw_book.get("publication_month"),
|
||||
publication_day=raw_book.get("publication_day"),
|
||||
)
|
||||
|
||||
try:
|
||||
reporter.start_download(book)
|
||||
result = download_book(
|
||||
def _make_download_fn():
|
||||
"""Closure capturing client, config, history, reporter for each book."""
|
||||
def _download_fn(book: Book):
|
||||
if verbose:
|
||||
console.print(f" ⬇ {book.title} [dim]({book.isbn})[/dim]")
|
||||
console.print(f" authors: {', '.join(book.authors) or '?'}")
|
||||
console.print(f" narrators: {', '.join(book.narrators) or '?'}")
|
||||
return download_book(
|
||||
book=book,
|
||||
client=client,
|
||||
output_base=config.output_dir,
|
||||
@@ -158,22 +134,20 @@ def run(
|
||||
config=config,
|
||||
progress=reporter.update,
|
||||
)
|
||||
if result is None:
|
||||
console.print(f" [yellow]⏭ Skipped[/yellow]")
|
||||
skipped += 1
|
||||
skipped_books.append(book)
|
||||
else:
|
||||
reporter.complete(book)
|
||||
if verbose:
|
||||
console.print(f" [dim] {result.stat().st_size:,} bytes[/dim]")
|
||||
downloaded += 1
|
||||
except Exception as exc:
|
||||
reporter.fail(book, reason=str(exc))
|
||||
if verbose:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
failed += 1
|
||||
failed_books.append((book, str(exc)))
|
||||
return _download_fn
|
||||
|
||||
result = download_all_books(
|
||||
new_books,
|
||||
workers=resolved_workers,
|
||||
download_fn=_make_download_fn(),
|
||||
reporter=reporter,
|
||||
)
|
||||
|
||||
downloaded = result.downloaded_count
|
||||
skipped = result.skipped_count
|
||||
failed = result.failed_count
|
||||
failed_books = result.failed_books
|
||||
skipped_books = result.skipped_books
|
||||
|
||||
reporter.summary(
|
||||
downloaded=downloaded,
|
||||
|
||||
Reference in new issue
Block a user