- MarshalJSON added for BaseTask.

This commit is contained in:
gazebo
2018-10-20 10:08:57 +08:00
parent c00336905a
commit 1c74a43c1b

View File

@@ -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()