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
+62 -1
View File
@@ -10,6 +10,7 @@ import (
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// taskPendingOpsRepository implements interfaces.TaskPendingOpsRepository.
@@ -26,6 +27,13 @@ func NewTaskPendingOpsRepository(db *gorm.DB) interfaces.TaskPendingOpsRepositor
// Scope/ScopeID/Op (Payload optional). ID, FailCount default to zero;
// EnqueuedAt is filled with the DB-side default if left zero.
func (r *taskPendingOpsRepository) Enqueue(ctx context.Context, op *types.TaskPendingOp) error {
if err := preparePendingOp(op); err != nil {
return err
}
return r.db.WithContext(ctx).Create(op).Error
}
func preparePendingOp(op *types.TaskPendingOp) error {
if op == nil {
return errors.New("task pending ops: nil op")
}
@@ -41,7 +49,48 @@ func (r *taskPendingOpsRepository) Enqueue(ctx context.Context, op *types.TaskPe
// driver-level default handling.
op.Payload = []byte("{}")
}
return r.db.WithContext(ctx).Create(op).Error
return nil
}
// EnqueueIfKnowledgeBaseActive prevents detached wiki cleanup from writing new
// durable work after a KB was soft-deleted. On Postgres the share lock
// serializes this check+insert transaction against the row update performed by
// soft deletion: whichever operation acquires the row first determines the
// order, and the deletion path's subsequent scope scrub removes any insert
// that committed before it.
func (r *taskPendingOpsRepository) EnqueueIfKnowledgeBaseActive(
ctx context.Context,
op *types.TaskPendingOp,
) (bool, error) {
if err := preparePendingOp(op); err != nil {
return false, err
}
if op.Scope != types.TaskScopeKnowledgeBase {
return false, errors.New("task pending ops: guarded enqueue requires knowledge_base scope")
}
accepted := false
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
query := tx.Model(&types.KnowledgeBase{}).
Select("id").
Where("id = ? AND tenant_id = ?", op.ScopeID, op.TenantID)
dialector := tx.Dialector
if dialector.Name() == "postgres" {
query = query.Clauses(clause.Locking{Strength: "SHARE"})
}
var kb types.KnowledgeBase
if err := query.Take(&kb).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
if err := tx.Create(op).Error; err != nil {
return err
}
accepted = true
return nil
})
return accepted, err
}
// PeekBatch returns up to `limit` rows for the (task_type, scope, scope_id)
@@ -228,6 +277,18 @@ func (r *taskPendingOpsRepository) DeleteByIDs(ctx context.Context, ids []int64)
Delete(&types.TaskPendingOp{}).Error
}
// DeleteByScope removes every pending operation owned by a scope, regardless
// of task type. Both scope fields are required so a malformed lifecycle call
// can never turn into an unbounded queue deletion.
func (r *taskPendingOpsRepository) DeleteByScope(ctx context.Context, scope, scopeID string) error {
if scope == "" || scopeID == "" {
return errors.New("task pending ops: scope and scope_id are required")
}
return r.db.WithContext(ctx).
Where("scope = ? AND scope_id = ?", scope, scopeID).
Delete(&types.TaskPendingOp{}).Error
}
// IncrFailCount atomically bumps fail_count for one row and returns the
// new value. We use UPDATE ... RETURNING so the read+write happens in
// one round trip and races between concurrent IncrFailCount callers