fix(api): lazy config reads, log ingest exceptions, suppress migrations in tests

This commit is contained in:
pyr0ball 2026-05-04 17:28:23 -07:00
parent 4c2370f1de
commit 751faf1679
2 changed files with 13 additions and 3 deletions

View file

@ -30,12 +30,17 @@ def _dispatch_ingest(
background_tasks: BackgroundTasks,
) -> str:
"""Dispatch an ingest task. Tries cf-orch; falls back to BackgroundTasks."""
import os as _os
from pathlib import Path as _Path
# Read lazily so test fixtures (monkeypatch.setenv) take effect
_data_dir = _Path(_os.environ.get("PAGEPIPER_DATA_DIR", "data"))
task_id = str(uuid.uuid4())
args = {
"doc_id": doc_id,
"file_path": file_path,
"db_path": DB_PATH,
"vec_db_path": VEC_DB_PATH,
"db_path": str(_data_dir / "pagepiper.db"),
"vec_db_path": str(_data_dir / "pagepiper_vecs.db"),
}
try:
@ -52,7 +57,7 @@ def _dispatch_ingest(
return task_id
def _run_ingest_background(run_fn, args: dict, task_id: str) -> None:
def _run_ingest_background(run_fn: Callable[..., None], args: dict, task_id: str) -> None:
from app.api.ingest import _task_registry
_task_registry[task_id] = {"status": "running", "progress": 0}
try:
@ -61,6 +66,7 @@ def _run_ingest_background(run_fn, args: dict, task_id: str) -> None:
if _mark_bm25_dirty:
_mark_bm25_dirty()
except Exception as exc:
logger.exception("Ingest task %s failed", task_id)
_task_registry[task_id] = {"status": "error", "error": str(exc)}

View file

@ -26,9 +26,13 @@ def client(test_db, tmp_path, monkeypatch):
monkeypatch.setenv("PAGEPIPER_WATCH_DIR", str(tmp_path / "books"))
(tmp_path / "books").mkdir(exist_ok=True)
import app.main as _main_module
from app.main import app, _bm25
from app.deps import get_db
# Suppress migrations during tests — test_db fixture already applies the schema
monkeypatch.setattr(_main_module, "_apply_migrations", lambda: None)
def override_db():
conn = sqlite3.connect(test_db)
conn.execute("PRAGMA foreign_keys = ON")