fix(summary): restore tenant context for refresh tasks

main
wizardchen 2026-07-28 16:12:49 +08:00 committed by lyingbug
parent 1b9858ecb8
commit 850a073c83
3 changed files with 87 additions and 1 deletions

View File

@ -943,7 +943,11 @@ func (s *knowledgeService) ProcessSummaryGeneration(ctx context.Context, t *asyn
return nil
}
if payload.Refresh {
_, err := s.RegenerateKnowledgeSummary(ctx, payload.KnowledgeID)
var err error
ctx, err = restoreSummaryRefreshTenantInfo(ctx, s.tenantRepo, payload.TenantID)
if err == nil {
_, err = s.RegenerateKnowledgeSummary(ctx, payload.KnowledgeID)
}
if err != nil {
// A retry will move the row back to processing. Keeping failed here
// ensures both Redis workers and the Lite executor have a terminal,

View File

@ -16,6 +16,28 @@ type summaryKnowledgeBaseReader interface {
GetKnowledgeBaseByID(ctx context.Context, id string) (*types.KnowledgeBase, error)
}
// restoreSummaryRefreshTenantInfo rebuilds the tenant context normally added
// by the HTTP authentication middleware. Summary refreshes run in an Asynq
// worker, and the retrieve-engine factory needs the full tenant configuration,
// not only TenantIDContextKey, when it reindexes the summary chunk.
func restoreSummaryRefreshTenantInfo(
ctx context.Context,
tenantRepo interfaces.TenantRepository,
tenantID uint64,
) (context.Context, error) {
if tenantRepo == nil {
return ctx, fmt.Errorf("tenant repository is unavailable")
}
tenant, err := tenantRepo.GetTenantByID(ctx, tenantID)
if err != nil {
return ctx, fmt.Errorf("get tenant %d for summary refresh: %w", tenantID, err)
}
if tenant == nil {
return ctx, fmt.Errorf("tenant %d not found for summary refresh", tenantID)
}
return context.WithValue(ctx, types.TenantInfoContextKey, tenant), nil
}
// enqueueSummaryRefresh marks an existing summary as queued and enqueues an
// independent refresh task. The pending status is written before Enqueue
// because the Lite executor may run the task synchronously inside Enqueue.

View File

@ -0,0 +1,60 @@
package service
import (
"context"
"errors"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
type summaryRefreshTenantRepo struct {
interfaces.TenantRepository
tenant *types.Tenant
err error
requestedID uint64
}
func (r *summaryRefreshTenantRepo) GetTenantByID(_ context.Context, id uint64) (*types.Tenant, error) {
r.requestedID = id
return r.tenant, r.err
}
func TestRestoreSummaryRefreshTenantInfo(t *testing.T) {
repo := &summaryRefreshTenantRepo{tenant: &types.Tenant{ID: 42}}
ctx, err := restoreSummaryRefreshTenantInfo(context.Background(), repo, 42)
if err != nil {
t.Fatalf("restore tenant context: %v", err)
}
if repo.requestedID != 42 {
t.Fatalf("requested tenant ID = %d, want 42", repo.requestedID)
}
tenant, ok := types.TenantInfoFromContext(ctx)
if !ok || tenant.ID != 42 {
t.Fatalf("tenant context = %#v, %v; want tenant 42", tenant, ok)
}
}
func TestRestoreSummaryRefreshTenantInfoRejectsMissingTenant(t *testing.T) {
tests := []struct {
name string
repo interfaces.TenantRepository
}{
{name: "repository unavailable"},
{name: "tenant lookup failed", repo: &summaryRefreshTenantRepo{err: errors.New("lookup failed")}},
{name: "tenant not found", repo: &summaryRefreshTenantRepo{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, err := restoreSummaryRefreshTenantInfo(context.Background(), tt.repo, 42)
if err == nil {
t.Fatal("expected an error")
}
if _, ok := types.TenantInfoFromContext(ctx); ok {
t.Fatal("tenant info should not be added after a failed lookup")
}
})
}
}