Slice 6: CLI integration — wire orchestrator + Ctrl+C drain #17

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

Parent

#11

What to build

Wire the parallel orchestrator into cli.py:run(), replacing the sequential for loop with orchestrator.run(). Add Ctrl+C graceful drain handling. This is the integration slice that makes parallel downloads end-to-end functional.

cli.py changes:

  • Replace lines 105-175 (the for raw_book in new_books: loop + summary call) with a call to the orchestrator
  • Pass resolved worker count (from Slice 1), book list, download callback (wrapping existing download_book() logic including Book construction), reporter, and config
  • The Book construction logic currently inside the loop (lines 116-139) moves into the per-book callback or the orchestrator's worker function
  • Verbose mode: each thread prints its own verbose output (already works since verbose lines use console.print which is thread-safe in Rich)

Ctrl+C handling:

  • Wrap executor block in try/except KeyboardInterrupt
  • On first Ctrl+C: print "Aborting..." immediately, then executor.shutdown(wait=True) to drain in-flight downloads, exit code 130
  • Partial files remain resume-safe (existing .partial + HTTP Range behavior)
  • On second Ctrl+C during drain: exit immediately (don't wait forever)
  • Move the existing outer KeyboardInterrupt handler (line 182) to be the orchestrator-level handler

Summary behavior:

  • Orchestrator returns ordered results; pass them directly to reporter.summary()
  • Grouping: succeeded first, then failed, then skipped -- each group in library order
  • Counts accurate even with millisecond-simultaneous completions

Regression guard:

  • --workers 1 must produce identical observable behavior to pre-parallelism version (same order, same output, same exit codes)

Acceptance criteria

  • python -m librofm_downloader --workers 3 downloads 3 books concurrently (integration test with mock)
  • --workers 1 produces identical results to sequential for-loop (regression test)
  • Ctrl+C during download prints "Aborting...", drains in-flight downloads, exits 130
  • Partial files after interrupt are resume-safe on next run
  • Summary shows downloaded/skipped/failed counts correctly grouped and ordered
  • Mixed result run (some succeed, some fail, some skip) reports each group correctly
  • Verbose mode output is readable when downloads overlap temporally
  • CLI flag --workers / -w shows in --help
  • 10-12 integration tests in test_cli.py: workers=1 parity, workers=3 concurrent, failure isolation, Ctrl+C drain, mixed results, verbose overlap, summary ordering
  • All 172+ existing tests still pass

Blocked by

  • #16 (Slice 5: Parallel orchestrator module - provides the orchestration engine to wire in)
## Parent #11 ## What to build Wire the parallel orchestrator into `cli.py:run()`, replacing the sequential `for` loop with `orchestrator.run()`. Add Ctrl+C graceful drain handling. This is the integration slice that makes parallel downloads end-to-end functional. **cli.py changes:** - Replace lines 105-175 (the `for raw_book in new_books:` loop + summary call) with a call to the orchestrator - Pass resolved worker count (from Slice 1), book list, download callback (wrapping existing `download_book()` logic including Book construction), reporter, and config - The Book construction logic currently inside the loop (lines 116-139) moves into the per-book callback or the orchestrator's worker function - Verbose mode: each thread prints its own verbose output (already works since verbose lines use `console.print` which is thread-safe in Rich) **Ctrl+C handling:** - Wrap executor block in `try/except KeyboardInterrupt` - On first Ctrl+C: print "Aborting..." immediately, then `executor.shutdown(wait=True)` to drain in-flight downloads, exit code 130 - Partial files remain resume-safe (existing .partial + HTTP Range behavior) - On second Ctrl+C during drain: exit immediately (don't wait forever) - Move the existing outer KeyboardInterrupt handler (line 182) to be the orchestrator-level handler **Summary behavior:** - Orchestrator returns ordered results; pass them directly to `reporter.summary()` - Grouping: succeeded first, then failed, then skipped -- each group in library order - Counts accurate even with millisecond-simultaneous completions **Regression guard:** - `--workers 1` must produce identical observable behavior to pre-parallelism version (same order, same output, same exit codes) ## Acceptance criteria - [ ] `python -m librofm_downloader --workers 3` downloads 3 books concurrently (integration test with mock) - [ ] `--workers 1` produces identical results to sequential for-loop (regression test) - [ ] Ctrl+C during download prints "Aborting...", drains in-flight downloads, exits 130 - [ ] Partial files after interrupt are resume-safe on next run - [ ] Summary shows downloaded/skipped/failed counts correctly grouped and ordered - [ ] Mixed result run (some succeed, some fail, some skip) reports each group correctly - [ ] Verbose mode output is readable when downloads overlap temporally - [ ] CLI flag `--workers` / `-w` shows in `--help` - [ ] 10-12 integration tests in `test_cli.py`: workers=1 parity, workers=3 concurrent, failure isolation, Ctrl+C drain, mixed results, verbose overlap, summary ordering - [ ] All 172+ existing tests still pass ## Blocked by - #16 (Slice 5: Parallel orchestrator module - provides the orchestration engine to wire in)
AlexKucera commented 2026-06-03 18:16:44 +02:00 (Migrated from github.com)

✅ Issue #17 complete — committed as f1ee0db and pushed to feature/parallel-downloads.

What was built:

  • Graceful Ctrl+C drain in orchestrator: first interrupt drains in-flight downloads, second force-quits
  • 13 new integration tests (231 total, all passing)
  • 10/10 acceptance criteria met

Test it: python -m librofm_downloader --workers 3 then press Ctrl+C during download.

✅ **Issue #17 complete** — committed as `f1ee0db` and pushed to `feature/parallel-downloads`. **What was built:** - Graceful Ctrl+C drain in orchestrator: first interrupt drains in-flight downloads, second force-quits - 13 new integration tests (231 total, all passing) - 10/10 acceptance criteria met **Test it:** `python -m librofm_downloader --workers 3` then press Ctrl+C during download.
Sign in to join this conversation.