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
- 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 |
-
|
798 |
-
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
f"
|
808 |
-
|
809 |
-
|
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 |
|