fix(wiki): Lite ingest lock, failed-op requeue, sync task retry parity

- Add per-KB sync.Map lock in Lite mode to mirror Redis SetNX concurrency.
- Requeue failed wiki ops in Lite by enqueueing asynq tasks with delay/tracing.
- Teach SyncTaskExecutor to honor ProcessIn delay and MaxRetry like Redis asynq.
This commit is contained in:
wizardchen
2026-04-28 18:08:20 +08:00
committed by lyingbug
parent 48c032ccbe
commit 1f03741462
3 changed files with 104 additions and 22 deletions
+50 -7
View File
@@ -36,7 +36,8 @@ func (e *SyncTaskExecutor) RegisterHandler(pattern string, handler func(context.
// Enqueue satisfies interfaces.TaskEnqueuer.
// Instead of queuing to Redis, it dispatches the task to a goroutine.
func (e *SyncTaskExecutor) Enqueue(task *asynq.Task, _ ...asynq.Option) (*asynq.TaskInfo, error) {
// Supports ProcessIn (delay) and MaxRetry options for parity with asynq.
func (e *SyncTaskExecutor) Enqueue(task *asynq.Task, opts ...asynq.Option) (*asynq.TaskInfo, error) {
e.mu.RLock()
handler, ok := e.handlers[task.Type()]
e.mu.RUnlock()
@@ -45,6 +46,28 @@ func (e *SyncTaskExecutor) Enqueue(task *asynq.Task, _ ...asynq.Option) (*asynq.
return nil, fmt.Errorf("sync task executor: no handler registered for type %q", task.Type())
}
var delay time.Duration
maxRetry := 25 // asynq default
maxRetrySet := false
for _, opt := range opts {
switch opt.Type() {
case asynq.ProcessInOpt:
if d, ok := opt.Value().(time.Duration); ok {
delay = d
}
case asynq.MaxRetryOpt:
if n, ok := opt.Value().(int); ok {
maxRetry = n
maxRetrySet = true
}
}
}
// Callers that explicitly pass MaxRetry(0) want no retries.
// Without the flag we can't distinguish "not set" from "set to 0".
if maxRetrySet && maxRetry < 0 {
maxRetry = 0
}
taskID := uuid.New().String()
info := &asynq.TaskInfo{
ID: taskID,
@@ -53,16 +76,36 @@ func (e *SyncTaskExecutor) Enqueue(task *asynq.Task, _ ...asynq.Option) (*asynq.
}
go func() {
if delay > 0 {
time.Sleep(delay)
}
ctx := context.Background()
start := time.Now()
logger.Infof(ctx, "[SyncTask] Executing task type=%s id=%s", task.Type(), taskID)
if err := handler(ctx, task); err != nil {
logger.Errorf(ctx, "[SyncTask] Task failed type=%s id=%s elapsed=%v err=%v",
task.Type(), taskID, time.Since(start), err)
} else {
logger.Infof(ctx, "[SyncTask] Task completed type=%s id=%s elapsed=%v",
task.Type(), taskID, time.Since(start))
var lastErr error
for attempt := 0; attempt <= maxRetry; attempt++ {
if attempt > 0 {
backoff := time.Duration(attempt) * 5 * time.Second
if backoff > 30*time.Second {
backoff = 30 * time.Second
}
logger.Infof(ctx, "[SyncTask] Retrying task type=%s id=%s attempt=%d/%d backoff=%s",
task.Type(), taskID, attempt, maxRetry, backoff)
time.Sleep(backoff)
}
lastErr = handler(ctx, task)
if lastErr == nil {
logger.Infof(ctx, "[SyncTask] Task completed type=%s id=%s elapsed=%v",
task.Type(), taskID, time.Since(start))
return
}
}
logger.Errorf(ctx, "[SyncTask] Task failed (exhausted retries) type=%s id=%s elapsed=%v err=%v",
task.Type(), taskID, time.Since(start), lastErr)
}()
return info, nil