mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-24 18:11:19 +01:00
fix(task): revert to context based approach and fix tasks stuck, improve error handling
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user