Add cancellation check in delete loop
This commit is contained in:
parent
743aefc655
commit
78ad8873b8
2 changed files with 19 additions and 2 deletions
|
|
@ -1556,7 +1556,19 @@ async def background_delete_documents(
|
||||||
try:
|
try:
|
||||||
# Loop through each document ID and delete them one by one
|
# Loop through each document ID and delete them one by one
|
||||||
for i, doc_id in enumerate(doc_ids, 1):
|
for i, doc_id in enumerate(doc_ids, 1):
|
||||||
|
# Check for cancellation at the start of each document deletion
|
||||||
async with pipeline_status_lock:
|
async with pipeline_status_lock:
|
||||||
|
if pipeline_status.get("cancellation_requested", False):
|
||||||
|
cancel_msg = f"Deletion cancelled by user at document {i}/{total_docs}. {len(successful_deletions)} deleted, {total_docs - i + 1} remaining."
|
||||||
|
logger.info(cancel_msg)
|
||||||
|
pipeline_status["latest_message"] = cancel_msg
|
||||||
|
pipeline_status["history_messages"].append(cancel_msg)
|
||||||
|
# Add remaining documents to failed list with cancellation reason
|
||||||
|
failed_deletions.extend(
|
||||||
|
doc_ids[i - 1 :]
|
||||||
|
) # i-1 because enumerate starts at 1
|
||||||
|
break # Exit the loop, remaining documents unchanged
|
||||||
|
|
||||||
start_msg = f"Deleting document {i}/{total_docs}: {doc_id}"
|
start_msg = f"Deleting document {i}/{total_docs}: {doc_id}"
|
||||||
logger.info(start_msg)
|
logger.info(start_msg)
|
||||||
pipeline_status["cur_batch"] = i
|
pipeline_status["cur_batch"] = i
|
||||||
|
|
@ -1719,6 +1731,10 @@ async def background_delete_documents(
|
||||||
# Final summary and check for pending requests
|
# Final summary and check for pending requests
|
||||||
async with pipeline_status_lock:
|
async with pipeline_status_lock:
|
||||||
pipeline_status["busy"] = False
|
pipeline_status["busy"] = False
|
||||||
|
pipeline_status["pending_requests"] = False # Reset pending requests flag
|
||||||
|
pipeline_status["cancellation_requested"] = (
|
||||||
|
False # Always reset cancellation flag
|
||||||
|
)
|
||||||
completion_msg = f"Deletion completed: {len(successful_deletions)} successful, {len(failed_deletions)} failed"
|
completion_msg = f"Deletion completed: {len(successful_deletions)} successful, {len(failed_deletions)} failed"
|
||||||
pipeline_status["latest_message"] = completion_msg
|
pipeline_status["latest_message"] = completion_msg
|
||||||
pipeline_status["history_messages"].append(completion_msg)
|
pipeline_status["history_messages"].append(completion_msg)
|
||||||
|
|
@ -2252,7 +2268,7 @@ def create_document_routes(
|
||||||
logger.error(traceback.format_exc())
|
logger.error(traceback.format_exc())
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
# TODO: Deprecated
|
# TODO: Deprecated, use /documents/paginated instead
|
||||||
@router.get(
|
@router.get(
|
||||||
"", response_model=DocsStatusesResponse, dependencies=[Depends(combined_auth)]
|
"", response_model=DocsStatusesResponse, dependencies=[Depends(combined_auth)]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
|
@ -68,7 +69,7 @@ from dotenv import load_dotenv
|
||||||
# use the .env that is inside the current folder
|
# use the .env that is inside the current folder
|
||||||
# allows to use different .env file for each lightrag instance
|
# allows to use different .env file for each lightrag instance
|
||||||
# the OS environment variables take precedence over the .env file
|
# the OS environment variables take precedence over the .env file
|
||||||
load_dotenv(dotenv_path=".env", override=False)
|
load_dotenv(dotenv_path=Path(__file__).resolve().parent / ".env", override=False)
|
||||||
|
|
||||||
|
|
||||||
def _truncate_entity_identifier(
|
def _truncate_entity_identifier(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue