fix(task): revert to context based approach and fix tasks stuck, improve error handling

This commit is contained in:
yusing
2025-05-26 00:32:59 +08:00
parent 2e9f113224
commit 216c03c5ff
11 changed files with 200 additions and 183 deletions

View File

@@ -1,23 +1,27 @@
package task
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/yusing/go-proxy/internal/gperr"
)
// debug only.
func (t *Task) listStuckedCallbacks() []string {
callbacks := make([]string, 0, len(t.callbacks))
for c := range t.callbacks {
if !c.done.Load() {
callbacks = append(callbacks, c.about)
}
t.mu.Lock()
defer t.mu.Unlock()
callbacks := make([]string, 0, len(t.callbacksOnFinish))
for c := range t.callbacksOnFinish {
callbacks = append(callbacks, c.about)
}
return callbacks
}
// debug only.
func (t *Task) listStuckedChildren() []string {
t.mu.Lock()
defer t.mu.Unlock()
children := make([]string, 0, len(t.children))
for c := range t.children {
if c.isFinished() {
@@ -34,13 +38,15 @@ func (t *Task) listStuckedChildren() []string {
func (t *Task) reportStucked() {
callbacks := t.listStuckedCallbacks()
children := t.listStuckedChildren()
fmtOutput := gperr.Multiline().
Addf("stucked callbacks: %d, stucked children: %d",
len(callbacks), len(children),
).
Addf("callbacks").
AddLinesString(callbacks...).
Addf("children").
AddLinesString(children...)
log.Warn().Msg(fmtOutput.Error())
if len(callbacks) == 0 && len(children) == 0 {
return
}
fmtOutput := gperr.NewBuilder(fmt.Sprintf("%s stucked callbacks: %d, stucked children: %d", t.String(), len(callbacks), len(children)))
if len(callbacks) > 0 {
fmtOutput.Add(gperr.New("callbacks").With(gperr.Multiline().AddLinesString(callbacks...)))
}
if len(children) > 0 {
fmtOutput.Add(gperr.New("children").With(gperr.Multiline().AddLinesString(children...)))
}
log.Warn().Msg(fmtOutput.String())
}