Slice 5: Parallel orchestrator module #16

Closed
opened 2026-06-03 16:47:28 +02:00 by AlexKucera · 1 comment
AlexKucera commented 2026-06-03 16:47:28 +02:00 (Migrated from github.com)

Parent

#11

What to build

New orchestrator.py module that encapsulates the ThreadPoolExecutor lifecycle for parallel book downloads. This is the core parallelism engine -- it replaces the sequential for loop in cli.py:run() with a thread-pool-based concurrent execution model.

Interface design:

  • Function or class accepting: list of (raw_book_dict) books, worker count, download callback (callable per book), reporter instance
  • Returns a result object/dataclass with: downloaded_count, skipped_count, failed_count, failed_books list, skipped_books list
  • Results grouped by status AND sorted back to original library order (stable sort by original index)

Execution model:

  • concurrent.futures.ThreadPoolExecutor(max_workers=N) wraps the existing synchronous download_book() call
  • Each future = one book's full download pipeline (API calls -> streaming -> history write)
  • as_completed() yields results as each finishes; per-future exceptions caught and recorded as failures
  • Sibling futures continue unaffected when one fails

Key behaviors:

  • workers=1 submits 1 at a time (semantically identical to sequential for loop)
  • One failure never blocks other books
  • Verbose mode output works correctly with temporal overlap (each thread prints its own verbose lines)
  • Summary counts are accurate even when multiple books complete within the same millisecond
  • Testable without network or filesystem: accepts a callable download callback, so tests can inject a mock/simulated download

Acceptance criteria

  • workers=1 produces identical sequential behavior and results as current for-loop
  • workers=3 submits all books as futures; multiple execute concurrently
  • One failed future does not cancel or block sibling futures
  • Results returned with correct counts (downloaded/skipped/failed)
  • Failed books returned with (book, reason) tuples; skipped books returned as Book objects
  • Results within each group sorted in original library order (stable sort)
  • Download callback is called once per book with correct arguments
  • Reporter start/complete/fail callbacks fire correctly per book
  • 12-15 new tests: sequential parity, concurrent submission, failure isolation, result grouping/ordering, callback wiring, verbose overlap
  • No network or filesystem required for orchestrator unit tests

Blocked by

  • #12 (Slice 1: Config workers field - needed for worker count parameter)
  • #13 (Slice 2: History thread-safety - needed for concurrent history writes)
  • #14 (Slice 3: ProgressReporter multi-bar - needed for concurrent progress reporting)
  • #15 (Slice 4: API rate limiter - needed to cap API concurrency inside worker threads)
## Parent #11 ## What to build New `orchestrator.py` module that encapsulates the `ThreadPoolExecutor` lifecycle for parallel book downloads. This is the core parallelism engine -- it replaces the sequential `for` loop in `cli.py:run()` with a thread-pool-based concurrent execution model. **Interface design:** - Function or class accepting: list of (raw_book_dict) books, worker count, download callback (callable per book), reporter instance - Returns a result object/dataclass with: `downloaded_count`, `skipped_count`, `failed_count`, `failed_books` list, `skipped_books` list - Results grouped by status AND sorted back to original library order (stable sort by original index) **Execution model:** - `concurrent.futures.ThreadPoolExecutor(max_workers=N)` wraps the existing synchronous `download_book()` call - Each future = one book's full download pipeline (API calls -> streaming -> history write) - `as_completed()` yields results as each finishes; per-future exceptions caught and recorded as failures - Sibling futures continue unaffected when one fails **Key behaviors:** - `workers=1` submits 1 at a time (semantically identical to sequential for loop) - One failure never blocks other books - Verbose mode output works correctly with temporal overlap (each thread prints its own verbose lines) - Summary counts are accurate even when multiple books complete within the same millisecond - Testable without network or filesystem: accepts a callable download callback, so tests can inject a mock/simulated download ## Acceptance criteria - [ ] `workers=1` produces identical sequential behavior and results as current for-loop - [ ] `workers=3` submits all books as futures; multiple execute concurrently - [ ] One failed future does not cancel or block sibling futures - [ ] Results returned with correct counts (downloaded/skipped/failed) - [ ] Failed books returned with (book, reason) tuples; skipped books returned as Book objects - [ ] Results within each group sorted in original library order (stable sort) - [ ] Download callback is called once per book with correct arguments - [ ] Reporter start/complete/fail callbacks fire correctly per book - [ ] 12-15 new tests: sequential parity, concurrent submission, failure isolation, result grouping/ordering, callback wiring, verbose overlap - [ ] No network or filesystem required for orchestrator unit tests ## Blocked by - #12 (Slice 1: Config workers field - needed for worker count parameter) - #13 (Slice 2: History thread-safety - needed for concurrent history writes) - #14 (Slice 3: ProgressReporter multi-bar - needed for concurrent progress reporting) - #15 (Slice 4: API rate limiter - needed to cap API concurrency inside worker threads)
AlexKucera commented 2026-06-03 18:01:47 +02:00 (Migrated from github.com)

✅ Issue #16 Complete — Parallel download orchestrator

What was built

  • **** — -based parallel download engine with:
    • →
    • dataclass: counts + stable-sorted /
    • conversion extracted from cli.py
    • Per-future failure isolation via
  • cli.py refactored — 55-line sequential for-loop → 23-line orchestrator call
  • 20 new tests (218 total) — all use injected , no network/filesystem needed

Acceptance Criteria: 11/11 ✅

# Criterion Status
1 workers=1 sequential parity ✅
2 workers=3 concurrent submission ✅
3 Failure isolation (one fail ≠ block siblings) ✅
4 Correct result counts ✅
5 Failed books as (book, reason) tuples ✅
6 Skipped books as Book objects ✅
7 Stable sort by original library order ✅
8 Download callback called once per book ✅
9 Reporter callbacks fire correctly ✅
10 12–15 new tests ✅ (20)
11 No network/filesystem for unit tests ✅

Commits: e7e6c62, c874eb3

✅ **Issue #16 Complete** — Parallel download orchestrator ### What was built - **** — -based parallel download engine with: - → - dataclass: counts + stable-sorted / - conversion extracted from cli.py - Per-future failure isolation via - **cli.py refactored** — 55-line sequential for-loop → 23-line orchestrator call - **20 new tests** (218 total) — all use injected , no network/filesystem needed ### Acceptance Criteria: 11/11 ✅ | # | Criterion | Status | |---|-----------|--------| | 1 | workers=1 sequential parity | ✅ | | 2 | workers=3 concurrent submission | ✅ | | 3 | Failure isolation (one fail ≠ block siblings) | ✅ | | 4 | Correct result counts | ✅ | | 5 | Failed books as (book, reason) tuples | ✅ | | 6 | Skipped books as Book objects | ✅ | | 7 | Stable sort by original library order | ✅ | | 8 | Download callback called once per book | ✅ | | 9 | Reporter callbacks fire correctly | ✅ | | 10 | 12–15 new tests | ✅ (20) | | 11 | No network/filesystem for unit tests | ✅ | Commits: `e7e6c62`, `c874eb3`
Sign in to join this conversation.