feat(task): print stack trace if panic

This commit is contained in:
Andy Hsu 2023-04-11 15:11:58 +08:00
parent 75fd0ee185
commit 86a773674a

View File

@ -3,6 +3,7 @@ package task
import (
"context"
"runtime"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
@ -63,11 +64,17 @@ func (t Task[K]) GetErrMsg() string {
return t.Error.Error()
}
func getCurrentGoroutineStack() string {
buf := make([]byte, 1<<16)
n := runtime.Stack(buf, false)
return string(buf[:n])
}
func (t *Task[K]) run() {
t.state = RUNNING
defer func() {
if err := recover(); err != nil {
log.Errorf("error [%+v] while run task [%s]", err, t.Name)
log.Errorf("error [%s] while run task [%s],stack trace:\n%s", err, t.Name, getCurrentGoroutineStack())
t.Error = errors.Errorf("panic: %+v", err)
t.state = ERRORED
}