fix(api): decrement knowledgebase counters on SDK re-parse / stop-parse (#17236)

This commit is contained in:
deadtrickster
2026-08-05 04:28:46 +02:00
committed by GitHub
parent 61d2747f6a
commit f063cfdb19
6 changed files with 446 additions and 81 deletions

View File

@@ -15,6 +15,7 @@
#
import logging
from api.db.services.document_counter_service import release_reparse_counters
from api.db.services.document_service import DocumentService
from api.db.services.file2document_service import File2DocumentService
from api.db.services.file_service import FileService
@@ -128,22 +129,14 @@ def reset_document_for_reparse(doc, tenant_id, parser_id=None, pipeline_id=None)
if not e:
return get_error_data_result(message="document not found")
# Update document statistics before deleting all document rows. Pipeline
# compilation rows may exist even when token_num is zero, so the doc-store
# cleanup must not be gated by the document counters.
if doc.token_num > 0:
try:
e = DocumentService.increment_chunk_num(
doc.id,
doc.kb_id,
doc.token_num * -1,
doc.chunk_num * -1,
doc.process_duration * -1,
)
except LookupError:
return get_error_data_result(message="document not found")
if not e:
return get_error_data_result(message="document not found")
# Release the document's chunk/token/duration counters from the knowledgebase
# aggregate under a row lock before clearing the chunks. release_reparse_counters
# guards the zero case internally, so the doc-store cleanup below still runs for
# pipeline compilation rows that exist even when token_num is zero.
try:
release_reparse_counters(doc.id)
except LookupError:
return get_error_data_result(message="Document not found!")
settings.docStoreConn.delete({"doc_id": doc.id}, search.index_name(tenant_id), doc.kb_id)
# Delete chunk images