2024-12-25 19:49:07 +08:00
|
|
|
import logging
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import click
|
2026-10-30 16:13:53 +08:00
|
|
|
from celery import Task, shared_task
|
2024-12-25 19:49:07 +08:00
|
|
|
|
2026-10-30 16:13:53 +08:00
|
|
|
from services.knowledge.indexing.locks import KnowledgeIndexingLockBusyError
|
2026-11-17 15:36:06 +08:00
|
|
|
from tasks.clean_document_task import (
|
|
|
|
|
DOCUMENT_CLEANUP_MAX_RETRIES,
|
|
|
|
|
_clean_documents,
|
|
|
|
|
document_cleanup_retries_exhausted,
|
|
|
|
|
)
|
|
|
|
|
from tasks.indexing_task_retry import TaskRetriesExhaustedError, is_final_attempt
|
2024-12-25 19:49:07 +08:00
|
|
|
|
2025-08-26 18:10:31 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-02-06 15:12:32 +08:00
|
|
|
|
2026-10-30 16:13:53 +08:00
|
|
|
@shared_task(
|
|
|
|
|
bind=True,
|
|
|
|
|
queue="dataset",
|
|
|
|
|
autoretry_for=(Exception,),
|
|
|
|
|
retry_backoff=True,
|
|
|
|
|
max_retries=None,
|
2026-11-17 15:36:06 +08:00
|
|
|
dont_autoretry_for=(ValueError, TaskRetriesExhaustedError),
|
2026-10-30 16:13:53 +08:00
|
|
|
acks_late=True,
|
|
|
|
|
reject_on_worker_lost=True,
|
|
|
|
|
)
|
2026-07-22 15:30:16 +08:00
|
|
|
def batch_clean_document_task(
|
2026-10-30 16:13:53 +08:00
|
|
|
self: Task,
|
2026-07-22 15:30:16 +08:00
|
|
|
document_ids: list[str],
|
|
|
|
|
dataset_id: str,
|
|
|
|
|
doc_form: str | None,
|
|
|
|
|
file_ids: list[str],
|
|
|
|
|
) -> None:
|
2026-11-17 15:36:06 +08:00
|
|
|
"""Clean a deleted document batch using the established four-field message.
|
|
|
|
|
|
|
|
|
|
Shares ``clean_document_task``'s retry contract: unbounded waits for a busy
|
|
|
|
|
index lock, a bounded budget for every other error, and relational cleanup
|
|
|
|
|
that still runs once index cleanup has failed terminally.
|
|
|
|
|
"""
|
2024-12-25 19:49:07 +08:00
|
|
|
|
2025-08-26 18:10:31 +08:00
|
|
|
logger.info(click.style("Start batch clean documents when documents deleted", fg="green"))
|
2024-12-25 19:49:07 +08:00
|
|
|
start_at = time.perf_counter()
|
2026-01-21 13:43:06 +08:00
|
|
|
if not doc_form:
|
|
|
|
|
raise ValueError("doc_form is required")
|
2026-11-17 15:36:06 +08:00
|
|
|
final_attempt = is_final_attempt(self, max_retries=DOCUMENT_CLEANUP_MAX_RETRIES)
|
2026-02-06 15:12:32 +08:00
|
|
|
try:
|
2026-10-30 16:13:53 +08:00
|
|
|
_clean_documents(
|
|
|
|
|
document_ids=document_ids,
|
|
|
|
|
dataset_id=dataset_id,
|
|
|
|
|
doc_form=doc_form,
|
|
|
|
|
file_ids=file_ids,
|
2026-11-17 15:36:06 +08:00
|
|
|
final_attempt=final_attempt,
|
2026-02-06 15:12:32 +08:00
|
|
|
)
|
2026-10-30 16:13:53 +08:00
|
|
|
except KnowledgeIndexingLockBusyError as error:
|
|
|
|
|
raise self.retry(exc=error, countdown=60, max_retries=None) from error
|
2026-11-17 15:36:06 +08:00
|
|
|
except ValueError:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as error:
|
|
|
|
|
if final_attempt:
|
|
|
|
|
raise document_cleanup_retries_exhausted(error, dataset_id=dataset_id) from error
|
|
|
|
|
raise
|
2026-10-30 16:13:53 +08:00
|
|
|
logger.info(
|
|
|
|
|
click.style(
|
|
|
|
|
f"Cleaned documents when documents deleted latency: {time.perf_counter() - start_at:.2f}s, "
|
|
|
|
|
f"dataset_id: {dataset_id}, document_ids: {document_ids}",
|
|
|
|
|
fg="green",
|
2026-02-06 15:12:32 +08:00
|
|
|
)
|
2026-10-30 16:13:53 +08:00
|
|
|
)
|