feat: debug api

This commit is contained in:
yusing
2025-04-13 06:17:41 +08:00
parent fdbf1ad787
commit 1eac48e899
8 changed files with 192 additions and 40 deletions

View File

@@ -1,7 +1,7 @@
package task
import (
"slices"
"iter"
"strings"
)
@@ -28,16 +28,43 @@ func (t *Task) listCallbacks() []string {
return callbacks
}
// DebugTaskList returns list of all tasks.
//
// The returned string is suitable for printing to the console.
func DebugTaskList() []string {
l := make([]string, 0, allTasks.Size())
allTasks.RangeAll(func(t *Task) {
l = append(l, t.name)
})
slices.Sort(l)
return l
func AllTasks() iter.Seq2[string, *Task] {
return func(yield func(k string, v *Task) bool) {
for t := range allTasks.Range {
if !yield(t.name, t) {
return
}
}
}
}
func (t *Task) Key() string {
return t.name
}
func toBool(v uint32) bool {
if v > 0 {
return true
}
return false
}
func (t *Task) callbackList() []map[string]any {
list := make([]map[string]any, 0, len(t.callbacks))
for cb, _ := range t.callbacks {
list = append(list, map[string]any{
"about": cb.about,
"wait_children": cb.waitChildren,
})
}
return list
}
func (t *Task) MarshalMap() map[string]any {
return map[string]any{
"name": t.name,
"childrens": t.children,
"callbacks": t.callbackList(),
"finishCalled": toBool(t.finishedCalled),
}
}