chore: 项目基线(基于 WeKnora 定制版,整理文件忽略规则)
This commit is contained in:
commit
9097ee699d
5769 files changed
+1446466
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package asynqdl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
// RecoverMiddleware turns a handler panic into an ordinary task error.
|
||||
// asynq recovers panics itself, but only in its processor, above every
|
||||
// middleware — so a panic skips the dead-letter callback and a document on
|
||||
// its last attempt stays "processing" until housekeeping. Install it right
|
||||
// after MiddlewareWithCallback so that callback sees the error.
|
||||
func RecoverMiddleware() asynq.MiddlewareFunc {
|
||||
return func(next asynq.Handler) asynq.Handler {
|
||||
return asynq.HandlerFunc(func(ctx context.Context, t *asynq.Task) error {
|
||||
return CallRecovered(ctx, t, next.ProcessTask)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// CallRecovered runs handler, converting a panic into an error.
|
||||
func CallRecovered(
|
||||
ctx context.Context, t *asynq.Task, handler func(context.Context, *asynq.Task) error,
|
||||
) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.Errorf(ctx, "task %s panicked: %v\n%s", t.Type(), r, debug.Stack())
|
||||
err = fmt.Errorf("task %s panicked: %v", t.Type(), r)
|
||||
}
|
||||
}()
|
||||
return handler(ctx, t)
|
||||
}
|
||||
Reference in new issue
Block a user