Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:1Panel dev MaxKB Celery QueueOnce Dedup Tip

From Leeroopedia
Knowledge Sources
Domains Async_Tasks, Optimization, RAG
Last Updated 2026-10-10 00:00 GMT

Overview

Use celery-once `QueueOnce` base class with per-entity lock keys to prevent duplicate concurrent embedding tasks for the same document or paragraph.

Description

MaxKB decorates all embedding-related Celery tasks with `base=QueueOnce` and specifies `once={"keys": [...]}` to create a Redis-backed lock per unique entity ID. This ensures that if a document or paragraph is submitted for embedding multiple times concurrently (e.g., due to user double-clicking or rapid API calls), only the first task executes. Subsequent tasks for the same key are skipped. The lock has a default timeout of 3600 seconds (1 hour), after which it auto-expires. This is critical because re-embedding the same paragraph concurrently would create duplicate vectors and waste embedding API quota.

Usage

Use this heuristic when debugging duplicate embedding issues or designing new Celery tasks in MaxKB. If a task silently does not execute (no logs, no errors), check whether a QueueOnce lock is held for the same key. When adding new async tasks, always use `base=QueueOnce` with appropriate `once` keys to prevent concurrent duplicates.

The Insight (Rule of Thumb)

  • Action: Decorate Celery tasks with `base=QueueOnce` and specify `once={"keys": ["<entity_id_field>"]}`.
  • Value: Use the entity's unique identifier as the lock key (e.g., `"document_id"`, `"paragraph_id"`, `"paragraph_id_list"`, `"knowledge_id"`).
  • Trade-off: Duplicate concurrent tasks are silently skipped (not queued). If the first task fails, the lock expires after `default_timeout` (3600s) before a retry is possible.
  • Compatibility: Requires Redis as the celery-once backend (configured in `CELERY_ONCE` settings). Works with both standalone Redis and Sentinel deployments.

Reasoning

Document embedding is idempotent (re-embedding produces the same vectors) but expensive (API calls, compute). If a user triggers embedding for the same document twice, running both tasks concurrently wastes resources and can cause database contention. celery-once creates a Redis key per lock; if the key exists, the task is skipped. The lock key is derived from the `once["keys"]` parameters — for example, `once={"keys": ["document_id"]}` means two calls with the same `document_id` cannot run concurrently. The `default_timeout` of 3600s prevents locks from being held forever if a worker crashes. Note that `embedding_by_document_list` (without QueueOnce) dispatches individual `embedding_by_document.delay()` calls, each of which IS protected by QueueOnce.

Code Evidence

QueueOnce on document embedding from `apps/knowledge/task/embedding.py:61-62`:

@celery_app.task(base=QueueOnce, once={"keys": ["document_id"]}, name="celery:embedding_by_document")
def embedding_by_document(document_id, model_id, state_list=None):

QueueOnce on paragraph embedding from `apps/knowledge/task/embedding.py:43-44`:

@celery_app.task(base=QueueOnce, once={"keys": ["paragraph_id"]}, name="celery:embedding_by_paragraph")
def embedding_by_paragraph(paragraph_id, model_id):

QueueOnce on paragraph list from `apps/knowledge/task/embedding.py:49-50`:

@celery_app.task(base=QueueOnce, once={"keys": ["paragraph_id_list"]}, name="celery:embedding_by_paragraph_data_list")
def embedding_by_paragraph_data_list(data_list, paragraph_id_list, model_id):

QueueOnce on knowledge-level embedding from `apps/knowledge/task/embedding.py:107-108`:

@celery_app.task(base=QueueOnce, once={"keys": ["knowledge_id"]}, name="celery:embedding_by_knowledge")
def embedding_by_knowledge(knowledge_id, model_id):

celery-once Redis config with lock timeout from `apps/maxkb/settings/lib.py:85-90`:

celery_once_settings['default_timeout'] = 3600  # 锁的默认超时时间(秒)
CELERY_ONCE = {
    'backend': 'celery_once.backends.Redis',
    'settings': celery_once_settings
}

Unprotected dispatcher (no QueueOnce, delegates to protected tasks) from `apps/knowledge/task/embedding.py:95-104`:

@celery_app.task(name="celery:embedding_by_document_list")
def embedding_by_document_list(document_id_list, model_id):
    for document_id in document_id_list:
        embedding_by_document.delay(document_id, model_id)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment