feat: Refactor knowledge base service and router for improved wiki handling
- Updated the knowledge base service to ensure WikiConfig is created when wiki indexing is enabled, enhancing backward compatibility. - Removed legacy syncing of WikiConfig.Enabled to IndexingStrategy.WikiEnabled, streamlining the logic for wiki enablement. - Introduced a custom retry delay function for asynq tasks to handle wiki ingest lock conflicts more effectively, improving task management. - Cleaned up the container file by removing unnecessary whitespace, contributing to better code readability.
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/application/service"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/hibiken/asynq"
|
||||
@@ -55,6 +57,29 @@ func NewAsyncqClient() (*asynq.Client, error) {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// wikiIngestRetryDelay is a fixed, short backoff for wiki ingest lock
|
||||
// conflicts. Must be slightly longer than the active-lock TTL's worst-case
|
||||
// "just got set" window so the retry is highly likely to succeed without
|
||||
// burning through retries; but short enough that users don't feel the stall.
|
||||
const wikiIngestRetryDelay = 15 * time.Second
|
||||
|
||||
// asynqRetryDelayFunc customizes per-task retry backoff.
|
||||
//
|
||||
// Default asynq backoff is exponential (≈10s, 40s, 90s, 2.5m, ...), which
|
||||
// is appropriate for transient errors like remote HTTP failures. But for
|
||||
// wiki ingest lock conflicts (ErrWikiIngestConcurrent), exponential
|
||||
// backoff is harmful: a freshly orphaned lock expires in ≤60s, so a 15s
|
||||
// fixed retry virtually guarantees the next attempt succeeds. Without
|
||||
// this override, a crash-restart cycle can leave a KB unable to make
|
||||
// progress for 7–10 minutes while the orphan lock expires AND the retry
|
||||
// schedule catches up.
|
||||
func asynqRetryDelayFunc(n int, e error, t *asynq.Task) time.Duration {
|
||||
if errors.Is(e, service.ErrWikiIngestConcurrent) {
|
||||
return wikiIngestRetryDelay
|
||||
}
|
||||
return asynq.DefaultRetryDelayFunc(n, e, t)
|
||||
}
|
||||
|
||||
func NewAsynqServer() *asynq.Server {
|
||||
opt := getAsynqRedisClientOpt()
|
||||
srv := asynq.NewServer(
|
||||
@@ -65,6 +90,7 @@ func NewAsynqServer() *asynq.Server {
|
||||
"default": 3, // Default priority queue
|
||||
"low": 1, // Lowest priority queue
|
||||
},
|
||||
RetryDelayFunc: asynqRetryDelayFunc,
|
||||
},
|
||||
)
|
||||
return srv
|
||||
|
||||
Reference in New Issue
Block a user