Fixed startup scan

This commit is contained in:
Saifeddine ALOUI
2025-01-25 00:17:22 +01:00
parent 34018cb1e0
commit c81da5a6d7

View File

@@ -674,18 +674,19 @@ def create_app(args):
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
"""Lifespan context manager for startup and shutdown events""" """Lifespan context manager for startup and shutdown events"""
# Startup logic # Startup logic
try: if args.auto_scan_at_startup:
new_files = doc_manager.scan_directory() try:
for file_path in new_files: new_files = doc_manager.scan_directory()
try: for file_path in new_files:
await index_file(file_path) try:
except Exception as e: await index_file(file_path)
trace_exception(e) except Exception as e:
logging.error(f"Error indexing file {file_path}: {str(e)}") trace_exception(e)
logging.error(f"Error indexing file {file_path}: {str(e)}")
logging.info(f"Indexed {len(new_files)} documents from {args.input_dir}") ASCIIColors.info(f"Indexed {len(new_files)} documents from {args.input_dir}")
except Exception as e: except Exception as e:
logging.error(f"Error during startup indexing: {str(e)}") logging.error(f"Error during startup indexing: {str(e)}")
yield yield
# Cleanup logic (if needed) # Cleanup logic (if needed)
pass pass
@@ -916,28 +917,6 @@ def create_app(args):
else: else:
logging.warning(f"No content extracted from file: {file_path}") logging.warning(f"No content extracted from file: {file_path}")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for startup and shutdown events"""
# Startup logic
# Now only if this option is active, we can scan. This is better for big databases where there are hundreds of
# files. Makes the startup faster
if args.auto_scan_at_startup:
ASCIIColors.info("Auto scan is active, rescanning the input directory.")
try:
new_files = doc_manager.scan_directory()
for file_path in new_files:
try:
await index_file(file_path)
except Exception as e:
trace_exception(e)
logging.error(f"Error indexing file {file_path}: {str(e)}")
logging.info(
f"Indexed {len(new_files)} documents from {args.input_dir}"
)
except Exception as e:
logging.error(f"Error during startup indexing: {str(e)}")
@app.post("/documents/scan", dependencies=[Depends(optional_api_key)]) @app.post("/documents/scan", dependencies=[Depends(optional_api_key)])
async def scan_for_new_documents(): async def scan_for_new_documents():