- refactor tasksch
- sequence task added. - task tree added.
This commit is contained in:
@@ -3,6 +3,9 @@ package tasksch
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -17,8 +20,10 @@ const (
|
||||
TaskStatusEnd = 4
|
||||
)
|
||||
|
||||
type TaskList []ITask
|
||||
|
||||
type ITask interface {
|
||||
Run() *ITask
|
||||
Run()
|
||||
GetResult(duration time.Duration) (retVal []interface{}, err error)
|
||||
Cancel()
|
||||
GetTotalItemCount() int
|
||||
@@ -26,6 +31,9 @@ type ITask interface {
|
||||
GetTotalJobCount() int
|
||||
GetFinishedJobCount() int
|
||||
GetStatus() int
|
||||
GetCreatedAt() time.Time
|
||||
|
||||
AddChild(task ITask)
|
||||
}
|
||||
|
||||
type BaseTask struct {
|
||||
@@ -46,24 +54,24 @@ type BaseTask struct {
|
||||
FailedJobCount int `json:"failedJobCount"`
|
||||
Status int `json:"status"`
|
||||
|
||||
Result []interface{} `json:"result"`
|
||||
Err error `json:"err"`
|
||||
Children TaskList `json:"children"`
|
||||
|
||||
finishChan chan int
|
||||
C <-chan int `json:"-"`
|
||||
params []interface{}
|
||||
quitChan chan int
|
||||
|
||||
locker sync.RWMutex
|
||||
result interface{}
|
||||
err error
|
||||
}
|
||||
|
||||
type TaskList []*Task
|
||||
|
||||
func (s TaskList) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func (s TaskList) Less(i, j int) bool {
|
||||
return s[i].CreatedAt.Sub(s[j].CreatedAt) < 0
|
||||
return s[i].GetCreatedAt().Sub(s[j].GetCreatedAt()) < 0
|
||||
}
|
||||
|
||||
func (s TaskList) Swap(i, j int) {
|
||||
@@ -72,10 +80,29 @@ func (s TaskList) Swap(i, j int) {
|
||||
s[j] = tmp
|
||||
}
|
||||
|
||||
func (t *BaseTask) Init(parallelCount, batchSize int, isContinueWhenError bool, params []interface{}, name, userName string, totalItemCount, totalJobCount int) {
|
||||
t.ID = utils.GetUUID()
|
||||
t.ParallelCount = parallelCount
|
||||
t.BatchSize = batchSize
|
||||
t.IsContinueWhenError = isContinueWhenError
|
||||
t.params = params
|
||||
t.Name = name
|
||||
t.CreatedAt = time.Now()
|
||||
t.CreatedBy = userName
|
||||
t.UpdatedAt = t.CreatedAt
|
||||
t.TerminatedAt = utils.DefaultTimeValue
|
||||
t.TotalItemCount = totalItemCount
|
||||
t.TotalJobCount = totalJobCount
|
||||
t.quitChan = make(chan int, parallelCount)
|
||||
t.finishChan = make(chan int, 2)
|
||||
t.Status = TaskStatusWorking
|
||||
|
||||
t.C = t.finishChan
|
||||
}
|
||||
|
||||
func (t *BaseTask) GetResult(duration time.Duration) (retVal []interface{}, err error) {
|
||||
if t.GetStatus() >= TaskStatusEndBegin {
|
||||
retVal, _ = t.result.([]interface{})
|
||||
return retVal, t.err
|
||||
return t.Result, t.Err
|
||||
}
|
||||
if duration == 0 {
|
||||
duration = time.Hour * 10000 // duration为0表示无限等待
|
||||
@@ -87,22 +114,32 @@ func (t *BaseTask) GetResult(duration time.Duration) (retVal []interface{}, err
|
||||
t.locker.RLock()
|
||||
defer t.locker.RUnlock()
|
||||
|
||||
retVal, _ = t.result.([]interface{})
|
||||
return retVal, t.err
|
||||
return t.Result, t.Err
|
||||
case <-timer.C:
|
||||
}
|
||||
return nil, ErrTaskNotFinished
|
||||
}
|
||||
|
||||
func (t *BaseTask) GetCreatedAt() time.Time {
|
||||
t.locker.RLock()
|
||||
defer t.locker.RUnlock()
|
||||
|
||||
return t.CreatedAt
|
||||
}
|
||||
|
||||
func (t *BaseTask) Cancel() {
|
||||
t.locker.Lock()
|
||||
defer t.locker.Unlock()
|
||||
if t.Status < TaskStatusEndBegin && t.Status != TaskStatusCanceling {
|
||||
t.Status = TaskStatusCanceling
|
||||
for i := 0; i < t.ParallelCount; i++ {
|
||||
t.quitChan <- 0
|
||||
}
|
||||
}
|
||||
t.locker.Unlock()
|
||||
|
||||
for _, subTask := range t.Children {
|
||||
subTask.Cancel()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *BaseTask) GetTotalItemCount() int {
|
||||
@@ -134,6 +171,24 @@ func (t *BaseTask) GetStatus() int {
|
||||
return t.Status
|
||||
}
|
||||
|
||||
func (t *BaseTask) AddChild(task ITask) {
|
||||
t.locker.Lock()
|
||||
defer t.locker.Unlock()
|
||||
|
||||
t.Children = append(t.Children, task)
|
||||
}
|
||||
|
||||
func (t *BaseTask) run(taskHandler func()) {
|
||||
go func() {
|
||||
taskHandler()
|
||||
for _, subTask := range t.Children {
|
||||
if _, err := subTask.GetResult(0); err != nil {
|
||||
globals.SugarLogger.Warnf("BaseTask run, failed with error:%v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
/////////
|
||||
func (t *BaseTask) finishedOneJob(itemCount int, err error) {
|
||||
t.locker.Lock()
|
||||
|
||||
Reference in New Issue
Block a user