From 1c74a43c1b1d423321b01cb9128223dae6d53d85 Mon Sep 17 00:00:00 2001 From: gazebo Date: Sat, 20 Oct 2018 10:08:57 +0800 Subject: [PATCH] - MarshalJSON added for BaseTask. --- business/jxutils/tasksch/task.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/business/jxutils/tasksch/task.go b/business/jxutils/tasksch/task.go index 05bb49412..cf22e6af9 100644 --- a/business/jxutils/tasksch/task.go +++ b/business/jxutils/tasksch/task.go @@ -1,6 +1,7 @@ package tasksch import ( + "encoding/json" "sync" "time" @@ -34,6 +35,9 @@ type ITask interface { GetCreatedAt() time.Time AddChild(task ITask) + GetChildren() TaskList + + json.Marshaler } type BaseTask struct { @@ -137,7 +141,8 @@ func (t *BaseTask) Cancel() { } t.locker.Unlock() - for _, subTask := range t.Children { + children := t.GetChildren() + for _, subTask := range children { subTask.Cancel() } } @@ -178,6 +183,26 @@ func (t *BaseTask) AddChild(task ITask) { t.Children = append(t.Children, task) } +func (t *BaseTask) GetChildren() (children TaskList) { + t.locker.RLock() + defer t.locker.RUnlock() + + if len(t.Children) > 0 { + children = make(TaskList, len(t.Children)) + copy(children, t.Children) + } + return children +} + +///////// + +func (t *BaseTask) MarshalJSON() ([]byte, error) { + type tBaseTask BaseTask + t.locker.RLock() + defer t.locker.RUnlock() + return utils.MustMarshal((*tBaseTask)(t)), nil +} + func (t *BaseTask) run(taskHandler func()) { go func() { taskHandler() @@ -189,7 +214,6 @@ func (t *BaseTask) run(taskHandler func()) { }() } -///////// func (t *BaseTask) finishedOneJob(itemCount int, err error) { t.locker.Lock() defer t.locker.Unlock()