Skip to content

Commit

Permalink
默认工作队列的错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Dec 10, 2022
1 parent 5cac6cd commit c9416f9
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"runtime"
"strings"
)
Expand All @@ -17,12 +18,18 @@ var (
}

// 默认工作队列, 64协程, 开启恢复模式
DefaultWorker = NewWorkerQueue(
DefaultWorkerQueue = NewWorkerQueue(
WithConcurrency(64),
WithRecovery(),
)
)

func init() {
DefaultWorkerQueue.OnError = func(err error) {
log.Printf(err.Error())
}
}

type (
Job struct {
Args interface{}
Expand Down Expand Up @@ -73,16 +80,16 @@ func WithRecovery() Option {
c.Caller = func(job Job) (err error) {
defer func() {
if fatalError := recover(); fatalError != nil {
var msg = make([]byte, 0, 128)
var msg = make([]byte, 0, 256)
msg = append(msg, fmt.Sprintf("fatal error: %v\n", fatalError)...)
for i := 1; true; i++ {
if _, caller, line, ok := runtime.Caller(i); ok {
if !strings.Contains(caller, "src/runtime") {
msg = append(msg, fmt.Sprintf("caller: %s, line: %d\n", caller, line)...)
}
} else {
_, caller, line, ok := runtime.Caller(i)
if !ok {
break
}
if !strings.Contains(caller, "src/runtime") {
msg = append(msg, fmt.Sprintf("caller: %s, line: %d\n", caller, line)...)
}
}
err = errors.New(string(msg))
}
Expand Down

0 comments on commit c9416f9

Please sign in to comment.