From 57da34533574e8242291adbc6e24fb249c0bb133 Mon Sep 17 00:00:00 2001 From: yusing Date: Mon, 14 Apr 2025 06:30:16 +0800 Subject: [PATCH] chore: enhance tasks debuggability --- internal/task/debug.go | 19 +++++++------------ internal/task/task.go | 12 +++++++----- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/internal/task/debug.go b/internal/task/debug.go index b2df5b52..97b30864 100644 --- a/internal/task/debug.go +++ b/internal/task/debug.go @@ -2,6 +2,7 @@ package task import ( "iter" + "strconv" "strings" ) @@ -42,19 +43,12 @@ 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, + "wait_children": strconv.FormatBool(cb.waitChildren), }) } return list @@ -62,9 +56,10 @@ func (t *Task) callbackList() []map[string]any { func (t *Task) MarshalMap() map[string]any { return map[string]any{ - "name": t.name, - "childrens": t.children, - "callbacks": t.callbackList(), - "finishCalled": toBool(t.finishedCalled), + "name": t.name, + "need_finish": strconv.FormatBool(t.needFinish), + "childrens": t.children, + "callbacks": t.callbackList(), + "finish_called": t.finishedCalled, } } diff --git a/internal/task/task.go b/internal/task/task.go index 2421a136..b93eae26 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -45,7 +45,8 @@ type ( callbacks map[*Callback]struct{} callbacksDone chan struct{} - finished chan struct{} + needFinish bool + finished chan struct{} // finishedCalled == 1 Finish has been called // but does not mean that the task is finished yet // this is used to avoid calling Finish twice @@ -145,10 +146,11 @@ func (t *Task) Subtask(name string, needFinish ...bool) *Task { ctx, cancel := context.WithCancelCause(t.ctx) child := &Task{ - parent: t, - finished: make(chan struct{}), - ctx: ctx, - cancel: cancel, + parent: t, + needFinish: nf, + finished: make(chan struct{}), + ctx: ctx, + cancel: cancel, } if t != root { child.name = t.name + "." + name