mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-11-29 00:16:21 +00:00
Pref: use redis to check if canceled. (#8853)
### What problem does this PR solve? ### Type of change - [x] Performance Improvement
This commit is contained in:
parent
ed8d7291ff
commit
aa4a725529
@ -32,7 +32,7 @@ from api.db.services.document_service import DocumentService, doc_upload_and_par
|
|||||||
from api.db.services.file2document_service import File2DocumentService
|
from api.db.services.file2document_service import File2DocumentService
|
||||||
from api.db.services.file_service import FileService
|
from api.db.services.file_service import FileService
|
||||||
from api.db.services.knowledgebase_service import KnowledgebaseService
|
from api.db.services.knowledgebase_service import KnowledgebaseService
|
||||||
from api.db.services.task_service import TaskService, queue_tasks
|
from api.db.services.task_service import TaskService, queue_tasks, cancel_all_task_of
|
||||||
from api.db.services.user_service import UserTenantService
|
from api.db.services.user_service import UserTenantService
|
||||||
from api.utils import get_uuid
|
from api.utils import get_uuid
|
||||||
from api.utils.api_utils import (
|
from api.utils.api_utils import (
|
||||||
@ -438,6 +438,9 @@ def run():
|
|||||||
if settings.docStoreConn.indexExist(search.index_name(tenant_id), doc.kb_id):
|
if settings.docStoreConn.indexExist(search.index_name(tenant_id), doc.kb_id):
|
||||||
settings.docStoreConn.delete({"doc_id": id}, search.index_name(tenant_id), doc.kb_id)
|
settings.docStoreConn.delete({"doc_id": id}, search.index_name(tenant_id), doc.kb_id)
|
||||||
|
|
||||||
|
if str(req["run"]) == TaskStatus.CANCEL.value:
|
||||||
|
cancel_all_task_of(id)
|
||||||
|
|
||||||
if str(req["run"]) == TaskStatus.RUNNING.value:
|
if str(req["run"]) == TaskStatus.RUNNING.value:
|
||||||
e, doc = DocumentService.get_by_id(id)
|
e, doc = DocumentService.get_by_id(id)
|
||||||
doc = doc.to_dict()
|
doc = doc.to_dict()
|
||||||
|
|||||||
@ -453,3 +453,20 @@ def reuse_prev_task_chunks(task: dict, prev_tasks: list[dict], chunking_config:
|
|||||||
prev_task["chunk_ids"] = ""
|
prev_task["chunk_ids"] = ""
|
||||||
|
|
||||||
return len(task["chunk_ids"].split())
|
return len(task["chunk_ids"].split())
|
||||||
|
|
||||||
|
|
||||||
|
def cancel_all_task_of(doc_id):
|
||||||
|
for t in TaskService.query(doc_id=doc_id):
|
||||||
|
try:
|
||||||
|
REDIS_CONN.set(f"{t.id}-cancel", "x")
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception(e)
|
||||||
|
|
||||||
|
|
||||||
|
def has_canceled(task_id):
|
||||||
|
try:
|
||||||
|
if REDIS_CONN.get(f"{task_id}-cancel"):
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception(e)
|
||||||
|
return False
|
||||||
|
|||||||
@ -50,7 +50,7 @@ from peewee import DoesNotExist
|
|||||||
from api.db import LLMType, ParserType
|
from api.db import LLMType, ParserType
|
||||||
from api.db.services.document_service import DocumentService
|
from api.db.services.document_service import DocumentService
|
||||||
from api.db.services.llm_service import LLMBundle
|
from api.db.services.llm_service import LLMBundle
|
||||||
from api.db.services.task_service import TaskService
|
from api.db.services.task_service import TaskService, has_canceled
|
||||||
from api.db.services.file2document_service import File2DocumentService
|
from api.db.services.file2document_service import File2DocumentService
|
||||||
from api import settings
|
from api import settings
|
||||||
from api.versions import get_ragflow_version
|
from api.versions import get_ragflow_version
|
||||||
@ -157,7 +157,7 @@ def set_progress(task_id, from_page=0, to_page=-1, prog=None, msg="Processing...
|
|||||||
try:
|
try:
|
||||||
if prog is not None and prog < 0:
|
if prog is not None and prog < 0:
|
||||||
msg = "[ERROR]" + msg
|
msg = "[ERROR]" + msg
|
||||||
cancel = TaskService.do_cancel(task_id)
|
cancel = has_canceled(task_id)
|
||||||
|
|
||||||
if cancel:
|
if cancel:
|
||||||
msg += " [Canceled]"
|
msg += " [Canceled]"
|
||||||
@ -214,7 +214,7 @@ async def collect():
|
|||||||
canceled = False
|
canceled = False
|
||||||
task = TaskService.get_task(msg["id"])
|
task = TaskService.get_task(msg["id"])
|
||||||
if task:
|
if task:
|
||||||
canceled = DocumentService.do_cancel(task["doc_id"])
|
canceled = has_canceled(task["id"])
|
||||||
if not task or canceled:
|
if not task or canceled:
|
||||||
state = "is unknown" if not task else "has been cancelled"
|
state = "is unknown" if not task else "has been cancelled"
|
||||||
FAILED_TASKS += 1
|
FAILED_TASKS += 1
|
||||||
@ -382,7 +382,7 @@ async def build_chunks(task, progress_callback):
|
|||||||
|
|
||||||
docs_to_tag = []
|
docs_to_tag = []
|
||||||
for d in docs:
|
for d in docs:
|
||||||
task_canceled = DocumentService.do_cancel(task["doc_id"])
|
task_canceled = has_canceled(task["id"])
|
||||||
if task_canceled:
|
if task_canceled:
|
||||||
progress_callback(-1, msg="Task has been canceled.")
|
progress_callback(-1, msg="Task has been canceled.")
|
||||||
return
|
return
|
||||||
@ -531,7 +531,7 @@ async def do_handle_task(task):
|
|||||||
progress_callback(-1, msg=error_message)
|
progress_callback(-1, msg=error_message)
|
||||||
raise Exception(error_message)
|
raise Exception(error_message)
|
||||||
|
|
||||||
task_canceled = DocumentService.do_cancel(task_doc_id)
|
task_canceled = has_canceled(task_id)
|
||||||
if task_canceled:
|
if task_canceled:
|
||||||
progress_callback(-1, msg="Task has been canceled.")
|
progress_callback(-1, msg="Task has been canceled.")
|
||||||
return
|
return
|
||||||
@ -609,7 +609,7 @@ async def do_handle_task(task):
|
|||||||
|
|
||||||
for b in range(0, len(chunks), DOC_BULK_SIZE):
|
for b in range(0, len(chunks), DOC_BULK_SIZE):
|
||||||
doc_store_result = await trio.to_thread.run_sync(lambda: settings.docStoreConn.insert(chunks[b:b + DOC_BULK_SIZE], search.index_name(task_tenant_id), task_dataset_id))
|
doc_store_result = await trio.to_thread.run_sync(lambda: settings.docStoreConn.insert(chunks[b:b + DOC_BULK_SIZE], search.index_name(task_tenant_id), task_dataset_id))
|
||||||
task_canceled = DocumentService.do_cancel(task_doc_id)
|
task_canceled = has_canceled(task_id)
|
||||||
if task_canceled:
|
if task_canceled:
|
||||||
progress_callback(-1, msg="Task has been canceled.")
|
progress_callback(-1, msg="Task has been canceled.")
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user