fix(tasks): dequeue work for deleted knowledge bases

This commit is contained in:
zhangzherui
2026-07-23 04:51:40 +08:00
committed by lyingbug
parent 7fde37eeeb
commit 37de0afad0
14 changed files with 1575 additions and 194 deletions
@@ -64,6 +64,24 @@ type TaskInspector interface {
WorkerServerStats(ctx context.Context) (stats []types.WorkerServerStat, supported bool, err error)
}
// KnowledgeBaseTaskCanceller is the optional queue-cleanup capability used
// when a knowledge base is deleted. It is separate from TaskInspector so
// lightweight test doubles and queue backends that cannot inspect tasks do
// not need to implement knowledge-base-wide scanning.
type KnowledgeBaseTaskCanceller interface {
// CancelTasksForKnowledgeBase removes pending/scheduled/retry tasks
// associated with kbID and signals matching active workers to stop.
// knowledgeIDs covers batch tasks whose payload references documents but
// does not carry the parent knowledge-base ID. dataSourceIDs covers sync
// tasks whose payload only identifies a data source.
CancelTasksForKnowledgeBase(
ctx context.Context,
kbID string,
knowledgeIDs []string,
dataSourceIDs []string,
) (deleted int, cancelled int, err error)
}
// RuntimeTaskInspector is the optional operator surface implemented by queue
// backends that retain inspectable task state. It is separate from
// TaskInspector so Lite mode and light-weight tests do not need to implement
+17
View File
@@ -83,6 +83,23 @@ type TaskPendingOpsRepository interface {
DeleteByDedupKey(ctx context.Context, taskType, scope, scopeID, dedupKey, op string) error
}
// TaskPendingOpsScopeCleaner is an optional extension for callers that need
// to discard every durable pending operation owned by a deleted scope. It is
// intentionally separate from TaskPendingOpsRepository so alternate queue
// implementations and existing test doubles are not forced to implement a
// lifecycle-only operation.
type TaskPendingOpsScopeCleaner interface {
DeleteByScope(ctx context.Context, scope, scopeID string) error
}
// TaskPendingOpsKnowledgeBaseGuard atomically persists a KB-scoped operation
// only while its knowledge base is still active. Implementations must
// serialize the active-KB check with soft deletion so a detached worker cannot
// recreate durable work after the deletion scrub has finished.
type TaskPendingOpsKnowledgeBaseGuard interface {
EnqueueIfKnowledgeBaseActive(ctx context.Context, op *types.TaskPendingOp) (accepted bool, err error)
}
// TaskDeadLetterRepository persists rows for the generic task dead-letter
// archive (`task_dead_letters`). Two writers exist: the asynq
// dead-letter middleware (one row per archived asynq task), and the
+1
View File
@@ -396,6 +396,7 @@ type KBDeletePayload struct {
TracingContext
TenantID uint64 `json:"tenant_id"`
KnowledgeBaseID string `json:"knowledge_base_id"`
DataSourceIDs []string `json:"data_source_ids,omitempty"`
EffectiveEngines []RetrieverEngineParams `json:"effective_engines"`
// VectorStoreID is the bound store snapshot taken at enqueue time (before
// soft-delete) so the async worker can resolve the right store. nil means