fix(knowledge): prevent documents from getting stuck in "processing"

Several failure modes left Knowledge.parse_status pinned at "processing"
forever, with no signal to users beyond a permanent spinner. This commit
addresses the root causes and adds a safety net.

- Asynq worker pool: explicit Concurrency (default 16, env-tunable via
  WEKNORA_ASYNQ_CONCURRENCY) so batch uploads don't queue behind a
  CPU-count-sized worker pool. Redis op timeouts raised to 500ms/1000ms
  (WEKNORA_REDIS_OP_TIMEOUT_MS) to absorb bursty multimodal counter ops.

- DocReader RPC: cap each call with WEKNORA_DOCREADER_CALL_TIMEOUT
  (default 30m). Without this, a hung docreader pinned a worker for the
  full DocumentProcessTimeout window.

- ImageMultimodal: finalize-on-last-attempt semantics. A permanently
  failing single image no longer strands the parent — the asynq retry
  is allowed to run, but on the final attempt we count the image
  regardless of outcome. Redis DECR errors fall back to enqueuing the
  post-process task instead of returning silently.

- Dead-letter callback: when DocumentProcess / KnowledgePostProcess /
  ManualProcess exhausts retries, immediately mark the corresponding
  Knowledge as failed with the last error. This surfaces the failure
  in the UI without waiting for the housekeeping sweep.

- HousekeepingService: 5-minute cron that flips knowledge rows stuck
  in "processing" past DocumentProcessTimeout + 10m to failed, plus
  summary rows stuck > 1h. Catches anything the other safety nets
  miss (worker SIGKILL mid-handler, etc.). Disable with
  WEKNORA_HOUSEKEEPING_ENABLED=false.

- Distributed startup recovery: previously the post-restart sweep was
  skipped whenever REDIS_ADDR was set, even though Asynq does not
  reschedule the task that was actively running on the dead instance.
  Now the sweep runs in distributed mode too, but only against rows
  older than 30 minutes to avoid racing peer instances.
This commit is contained in:
wizardchen
2026-05-27 13:17:08 +08:00
committed by lyingbug
parent ae4ec0cf06
commit 3ae3ea97c5
8 changed files with 536 additions and 65 deletions
+46 -23
View File
@@ -46,7 +46,22 @@ import (
//
// This middleware should be installed once on the asynq mux, BEFORE
// other middlewares that may transform errors. See router/task.go.
func Middleware(repo interfaces.TaskDeadLetterRepository) asynq.MiddlewareFunc {
// OnDeadLetter is an optional callback invoked AFTER the dead-letter row has
// been recorded. It exists so callers can flip user-visible state alongside
// the bookkeeping row — e.g. mark a Knowledge as failed when its document
// processing task is permanently archived. Without this hook the only signal
// to the UI is the timestamp on the knowledge row going stale, which is what
// produced "stuck in processing forever" reports.
//
// The callback receives the raw asynq Task and the final error. It runs with
// context.Background() (NOT the task ctx) so a cancelled task ctx during
// shutdown doesn't also drop the state update. Errors are logged and
// swallowed; the callback must NEVER alter the original task error.
type OnDeadLetter func(ctx context.Context, t *asynq.Task, taskErr error)
// MiddlewareWithCallback is the extended form of Middleware. The callback may
// be nil, in which case behaviour matches Middleware exactly.
func MiddlewareWithCallback(repo interfaces.TaskDeadLetterRepository, cb OnDeadLetter) asynq.MiddlewareFunc {
return func(next asynq.Handler) asynq.Handler {
return asynq.HandlerFunc(func(ctx context.Context, t *asynq.Task) error {
err := next.ProcessTask(ctx, t)
@@ -56,39 +71,47 @@ func Middleware(repo interfaces.TaskDeadLetterRepository) asynq.MiddlewareFunc {
if !isFinalAttempt(ctx) {
return err
}
if repo == nil {
return err
}
// Capture the actual attempt count from asynq so the
// dead-letter row reflects how many tries were made
// before the task was archived. On the final attempt
// asynq's counter satisfies retried == maxRetry, so the
// total attempt count is retried + 1 (initial try +
// retries). Both helpers can fail outside a worker ctx;
// in that case we record 0 so it's clear the count
// wasn't observable.
attempts := 0
if retried, ok := asynq.GetRetryCount(ctx); ok {
attempts = retried + 1
}
dl := buildDeadLetter(t, err, attempts)
if dl == nil {
return err
if repo != nil {
dl := buildDeadLetter(t, err, attempts)
if dl != nil {
if insertErr := repo.Insert(context.Background(), dl); insertErr != nil {
logger.Warnf(ctx,
"asynq dead-letter: failed to record %s task: %v (original task error: %v)",
t.Type(), insertErr, err,
)
}
}
}
// context.Background() so a cancelled task ctx (e.g. server
// shutdown signaling cancellation right as a task fails)
// doesn't also drop the dead-letter record.
if insertErr := repo.Insert(context.Background(), dl); insertErr != nil {
logger.Warnf(ctx,
"asynq dead-letter: failed to record %s task: %v (original task error: %v)",
t.Type(), insertErr, err,
)
if cb != nil {
// Wrap callback so a panic in user code doesn't escape
// into asynq's worker goroutine.
safeInvokeCallback(ctx, t, err, cb)
}
return err
})
}
}
func safeInvokeCallback(ctx context.Context, t *asynq.Task, taskErr error, cb OnDeadLetter) {
defer func() {
if r := recover(); r != nil {
logger.Warnf(ctx, "asynq dead-letter callback panicked for %s: %v", t.Type(), r)
}
}()
cb(context.Background(), t, taskErr)
}
// Middleware preserves the original signature for callers that don't need
// the dead-letter state-linkback hook. Equivalent to MiddlewareWithCallback
// with a nil callback.
func Middleware(repo interfaces.TaskDeadLetterRepository) asynq.MiddlewareFunc {
return MiddlewareWithCallback(repo, nil)
}
// isFinalAttempt reports whether the just-finished handler invocation
// was the LAST one asynq will run before archiving the task. Asynq
// increments retry_count *after* a successful retry, so on the final