yangdx commited on
Commit
6475955
·
1 Parent(s): afc3b40

Move document scanning trigger by command line to background task

Browse files

- Added background task management
- Prevented concurrent scanning
- Tracked scanning progress
- Improved startup performance
- Enhanced error handling

Files changed (1) hide show
  1. lightrag/api/lightrag_server.py +16 -15
lightrag/api/lightrag_server.py CHANGED
@@ -6,7 +6,7 @@ from fastapi import (
6
  Form,
7
  BackgroundTasks,
8
  )
9
-
10
  import threading
11
  import os
12
  import json
@@ -730,6 +730,8 @@ def create_app(args):
730
  postgres_db = None
731
  oracle_db = None
732
  tidb_db = None
 
 
733
 
734
  try:
735
  # Check if PostgreSQL is needed
@@ -794,20 +796,19 @@ def create_app(args):
794
 
795
  # Auto scan documents if enabled
796
  if args.auto_scan_at_startup:
797
- try:
798
- new_files = doc_manager.scan_directory_for_new_files()
799
- for file_path in new_files:
800
- try:
801
- await index_file(file_path)
802
- except Exception as e:
803
- trace_exception(e)
804
- logging.error(f"Error indexing file {file_path}: {str(e)}")
805
-
806
- ASCIIColors.info(
807
- f"Indexed {len(new_files)} documents from {args.input_dir}"
808
- )
809
- except Exception as e:
810
- logging.error(f"Error during startup indexing: {str(e)}")
811
 
812
  yield
813
 
 
6
  Form,
7
  BackgroundTasks,
8
  )
9
+ import asyncio
10
  import threading
11
  import os
12
  import json
 
730
  postgres_db = None
731
  oracle_db = None
732
  tidb_db = None
733
+ # Store background tasks
734
+ app.state.background_tasks = set()
735
 
736
  try:
737
  # Check if PostgreSQL is needed
 
796
 
797
  # Auto scan documents if enabled
798
  if args.auto_scan_at_startup:
799
+ # Start scanning in background
800
+ with progress_lock:
801
+ if not scan_progress["is_scanning"]:
802
+ scan_progress["is_scanning"] = True
803
+ scan_progress["indexed_count"] = 0
804
+ scan_progress["progress"] = 0
805
+ # Create background task
806
+ task = asyncio.create_task(run_scanning_process())
807
+ app.state.background_tasks.add(task)
808
+ task.add_done_callback(app.state.background_tasks.discard)
809
+ ASCIIColors.info(f"Started background scanning of documents from {args.input_dir}")
810
+ else:
811
+ ASCIIColors.info("Skip document scanning cause anohter scanning is active")
 
812
 
813
  yield
814