Skip to content

Commit 8dbe465

Browse files
CopilotMte90
andcommitted
Use FastAPI BackgroundTasks and test sqlite-vector at startup
Co-authored-by: Mte90 <403283+Mte90@users.noreply.github.com>
1 parent 99a3fc7 commit 8dbe465

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

ai/analyzer.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,15 @@ def analyze_local_path_sync(
422422

423423
def analyze_local_path_background(local_path: str, database_path: str, venv_path: Optional[str] = None, max_file_size: int = 200000, cfg: Optional[dict] = None):
424424
"""
425-
Non-blocking wrapper intended to be scheduled by FastAPI BackgroundTasks.
426-
This function starts a daemon thread which runs the synchronous analyzer and returns immediately.
425+
Wrapper intended to be scheduled by FastAPI BackgroundTasks.
426+
This function runs the synchronous analyzer in the FastAPI background task.
427427
Usage from FastAPI endpoint:
428428
background_tasks.add_task(analyze_local_path_background, local_path, database_path, venv_path, max_file_size, cfg)
429429
"""
430-
def _worker():
431-
try:
432-
analyze_local_path_sync(local_path, database_path, venv_path=venv_path, max_file_size=max_file_size, cfg=cfg)
433-
except Exception:
434-
logger.exception("Background analysis worker failed for %s", local_path)
435-
436-
t = threading.Thread(target=_worker, name=f"analysis-worker-{os.path.basename(local_path)}", daemon=True)
437-
t.start()
430+
try:
431+
analyze_local_path_sync(local_path, database_path, venv_path=venv_path, max_file_size=max_file_size, cfg=cfg)
432+
except Exception:
433+
logger.exception("Background analysis worker failed for %s", local_path)
438434

439435

440436
# Simple synchronous helpers preserved for compatibility --------------------------------

main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from fastapi.staticfiles import StaticFiles
77
from contextlib import asynccontextmanager
88
import os
9+
import tempfile
910
import uvicorn
1011

1112
from db import operations as db_operations
@@ -28,6 +29,32 @@ async def lifespan(app: FastAPI):
2829
"""Application lifespan handler."""
2930
global _file_watcher
3031

32+
# Test sqlite-vector extension loading at startup
33+
logger.info("Testing sqlite-vector extension loading...")
34+
try:
35+
from db.vector_operations import connect_db, load_sqlite_vector_extension
36+
37+
# Create a temporary database to test the extension
38+
with tempfile.NamedTemporaryFile(suffix='.db', delete=False) as tmp:
39+
tmp_db_path = tmp.name
40+
41+
try:
42+
conn = connect_db(tmp_db_path)
43+
try:
44+
load_sqlite_vector_extension(conn)
45+
logger.info("✓ sqlite-vector extension loaded successfully")
46+
finally:
47+
conn.close()
48+
finally:
49+
# Clean up temporary database
50+
try:
51+
os.unlink(tmp_db_path)
52+
except Exception:
53+
pass
54+
except Exception as e:
55+
logger.error(f"FATAL: Failed to load sqlite-vector extension at startup: {e}")
56+
raise RuntimeError(f"Cannot start application: sqlite-vector extension failed to load. {e}") from e
57+
3158
# Project registry is auto-initialized when needed via create_project
3259

3360
# Auto-create default project from configured local_path if it exists

0 commit comments

Comments
 (0)