Resumable job runner with progress in SQLite (#15) #55

Merged
chris-kreidl merged 1 commit from 15-resumable-jobs into main 2026-11-12 18:04:38 +00:00
chris-kreidl commented 2026-11-12 16:41:49 +00:00 (Migrated from github.com)

🤖 Claude: Closes #15

Each run of a step is now a row in a jobs table. A run that's interrupted resumes where it stopped, and only one run of a step can be active at a time.

What changed

  • src/jobs.ts (new): startJob records a running row (pid, start time) and writes progress (done/total/latest message) every second. That write also serves as a heartbeat. At the next start, a running row is marked interrupted if its process is gone or it hasn't written for 60 s. The check and the insert run in one BEGIN IMMEDIATE transaction, so two processes can't both start the same step. Steps save finished work to job_checkpoint. A run that resumes an interrupted run gets that work back. Any other run clears it, and a success clears it too.
  • Refresh: an incremental run saves whether each 10-K was found. A resumed run skips companies whose 10-Ks were all checked. The bulk zip resumes with Range + If-Range (ETag). If SEC sends the whole file instead, the download starts over and says so. If the run died after the download finished, during extraction, the zip isn't fetched again. SecFetch now takes extra request headers.
  • Enrich: each quote batch's answers, including "no quote", are saved. A resumed run asks Yahoo only for the rest. The table is still written in one transaction at the end.
  • CLI: --restart starts an interrupted refresh or enrich over. In the menu you choose "Resume it" or "Start over". Each hint shows the step's last run. Before anything runs, any step that's already active is named, with job id and pid, and the CLI exits 1. Analyze and rank record jobs but always start over (your decision on #15).
  • Ctrl-C fix: clack's spinner caught SIGINT/SIGTERM to redraw itself and never exited, so Ctrl-C during a progress bar did nothing. It now exits 130. I found this in the real-data demo.
  • jobs and job_checkpoint are in ensureSchema. All history is kept. CLAUDE.md is updated.

Demonstrated on real data

  • Kill enrich halfway: Ctrl-C at batch 8 of 16 left job 2 running, with 900 quotes saved. The re-run printed Resuming the interrupted run (job 2) and priced only the remaining 693 tickers (7 batches, 7 s). The totals were identical to an uninterrupted run: 1591 priced · 2 no quote · 900 quotes from the interrupted run. Job 2 became interrupted, job 3 succeeded with resumes = 2, and the checkpoint was cleared.
  • Bulk download: Ctrl-C at 283 of 1358 MB. The re-run printed Downloaded 1358 MB (resumed at 290 MB) and extracted 20,571 files from the appended zip.
  • One at a time: a second bun cli.ts enrich while one was running printed enrich is already running (job 4, pid 83181, started …) and exited 1.
  • --restart after an interruption fetched all 16 batches again and resumed nothing.
  • Hints: the menu showed Enrich with prices (1679 companies · about 2 min · last run succeeded Nov 12, 2026 at 11:36 AM).
  • bun run pipeline: all four steps ran and recorded succeeded jobs.

For your review

  1. Failed runs don't resume, only interrupted ones (per the criteria). For example, a network error at 900 MB means the next run starts the bulk download over. Resuming after failed too would be a one-line change. Your call; I can open an issue.
  2. A resumed enrich reuses quotes however old the interrupted run is. --restart gives fresh prices. Prices that are days old could warrant a limit; that's also your call.

Definition of Done

  • Acceptance criteria met and demonstrated (above)
  • bun test passes; new logic has tests: 263 pass, 0 fail. New: test/jobs.test.ts (16), resume tests for enrich (3), refresh incremental (1) and bulk (4), and CLI (3)
  • bun check is clean and bun fmt --check passes
  • Runs end to end on real data (not just fixtures): bun run pipeline, plus the kill/resume runs above
  • Stand-up posted on the milestone tracking issue when the story started and when this PR opened (#50)

🤖 Generated with Claude Code

🤖 **Claude:** Closes #15 Each run of a step is now a row in a `jobs` table. A run that's interrupted resumes where it stopped, and only one run of a step can be active at a time. ### What changed - **`src/jobs.ts` (new):** `startJob` records a `running` row (pid, start time) and writes progress (done/total/latest message) every second. That write also serves as a heartbeat. At the next start, a `running` row is marked `interrupted` if its process is gone or it hasn't written for 60 s. The check and the insert run in one `BEGIN IMMEDIATE` transaction, so two processes can't both start the same step. Steps save finished work to `job_checkpoint`. A run that resumes an interrupted run gets that work back. Any other run clears it, and a success clears it too. - **Refresh:** an incremental run saves whether each 10-K was found. A resumed run skips companies whose 10-Ks were all checked. The bulk zip resumes with `Range` + `If-Range` (ETag). If SEC sends the whole file instead, the download starts over and says so. If the run died after the download finished, during extraction, the zip isn't fetched again. `SecFetch` now takes extra request headers. - **Enrich:** each quote batch's answers, including "no quote", are saved. A resumed run asks Yahoo only for the rest. The table is still written in one transaction at the end. - **CLI:** `--restart` starts an interrupted refresh or enrich over. In the menu you choose "Resume it" or "Start over". Each hint shows the step's last run. Before anything runs, any step that's already active is named, with job id and pid, and the CLI exits 1. Analyze and rank record jobs but always start over (your decision on #15). - **Ctrl-C fix:** clack's spinner caught SIGINT/SIGTERM to redraw itself and never exited, so Ctrl-C during a progress bar did nothing. It now exits 130. I found this in the real-data demo. - `jobs` and `job_checkpoint` are in `ensureSchema`. All history is kept. CLAUDE.md is updated. ### Demonstrated on real data - **Kill enrich halfway:** Ctrl-C at batch 8 of 16 left job 2 `running`, with 900 quotes saved. The re-run printed `Resuming the interrupted run (job 2)` and priced only the remaining 693 tickers (7 batches, 7 s). The totals were identical to an uninterrupted run: `1591 priced · 2 no quote · 900 quotes from the interrupted run`. Job 2 became `interrupted`, job 3 succeeded with `resumes = 2`, and the checkpoint was cleared. - **Bulk download:** Ctrl-C at 283 of 1358 MB. The re-run printed `Downloaded 1358 MB (resumed at 290 MB)` and extracted 20,571 files from the appended zip. - **One at a time:** a second `bun cli.ts enrich` while one was running printed `enrich is already running (job 4, pid 83181, started …)` and exited 1. - **`--restart`** after an interruption fetched all 16 batches again and resumed nothing. - **Hints:** the menu showed `Enrich with prices (1679 companies · about 2 min · last run succeeded Nov 12, 2026 at 11:36 AM)`. - **`bun run pipeline`:** all four steps ran and recorded `succeeded` jobs. ### For your review 1. **Failed runs don't resume, only interrupted ones** (per the criteria). For example, a network error at 900 MB means the next run starts the bulk download over. Resuming after `failed` too would be a one-line change. Your call; I can open an issue. 2. **A resumed enrich reuses quotes however old the interrupted run is.** `--restart` gives fresh prices. Prices that are days old could warrant a limit; that's also your call. ### Definition of Done - [x] Acceptance criteria met and demonstrated (above) - [x] `bun test` passes; new logic has tests: 263 pass, 0 fail. New: `test/jobs.test.ts` (16), resume tests for enrich (3), refresh incremental (1) and bulk (4), and CLI (3) - [x] `bun check` is clean and `bun fmt --check` passes - [x] Runs end to end on real data (not just fixtures): `bun run pipeline`, plus the kill/resume runs above - [x] Stand-up posted on the milestone tracking issue when the story started and when this PR opened (#50) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
claude[bot] commented 2026-11-12 16:42:45 +00:00 (Migrated from github.com)

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

## Code review No issues found. Checked for bugs and CLAUDE.md compliance.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ckreidl/screener!55
No description provided.