1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 07:30:55 +00:00

make code testable in task module

This commit is contained in:
Anker Jam 2020-10-05 10:13:29 +08:00
parent f1cca45d8d
commit 70cca5e298
3 changed files with 99 additions and 54 deletions

View File

@ -28,8 +28,8 @@ type listTaskCommand struct {
} }
func (l *listTaskCommand) Execute(params ...interface{}) *governor.Result { func (l *listTaskCommand) Execute(params ...interface{}) *governor.Result {
resultList := make([][]string, 0, len(AdminTaskList)) resultList := make([][]string, 0, len(globalTaskManager.adminTaskList))
for tname, tk := range AdminTaskList { for tname, tk := range globalTaskManager.adminTaskList {
result := []string{ result := []string{
template.HTMLEscapeString(tname), template.HTMLEscapeString(tname),
template.HTMLEscapeString(tk.GetSpec(nil)), template.HTMLEscapeString(tk.GetSpec(nil)),
@ -65,7 +65,7 @@ func (r *runTaskCommand) Execute(params ...interface{}) *governor.Result {
} }
} }
if t, ok := AdminTaskList[tn]; ok { if t, ok := globalTaskManager.adminTaskList[tn]; ok {
err := t.Run(context.Background()) err := t.Run(context.Background())
if err != nil { if err != nil {
return &governor.Result{ return &governor.Result{

View File

@ -31,13 +31,28 @@ type bounds struct {
names map[string]uint names map[string]uint
} }
// The bounds for each field. type taskManager struct {
var ( adminTaskList map[string]Tasker
AdminTaskList map[string]Tasker
taskLock sync.RWMutex taskLock sync.RWMutex
stop chan bool stop chan bool
changed chan bool changed chan bool
isstart bool isstart bool
}
func newTaskManager()*taskManager{
return &taskManager{
adminTaskList: make(map[string]Tasker),
taskLock: sync.RWMutex{},
stop: make(chan bool),
changed: make(chan bool),
isstart: false,
}
}
// The bounds for each field.
var (
globalTaskManager *taskManager
seconds = bounds{0, 59, nil} seconds = bounds{0, 59, nil}
minutes = bounds{0, 59, nil} minutes = bounds{0, 59, nil}
hours = bounds{0, 23, nil} hours = bounds{0, 23, nil}
@ -398,32 +413,53 @@ func dayMatches(s *Schedule, t time.Time) bool {
// StartTask start all tasks // StartTask start all tasks
func StartTask() { func StartTask() {
taskLock.Lock() globalTaskManager.StartTask()
defer taskLock.Unlock() }
if isstart {
// StopTask stop all tasks
func StopTask() {
globalTaskManager.StopTask()
}
// AddTask add task with name
func AddTask(taskName string, t Tasker) {
globalTaskManager.AddTask(taskName, t)
}
// DeleteTask delete task with name
func DeleteTask(taskName string) {
globalTaskManager.DeleteTask(taskName)
}
// StartTask start all tasks
func (m *taskManager) StartTask() {
m.taskLock.Lock()
defer m.taskLock.Unlock()
if m.isstart {
// If already started no need to start another goroutine. // If already started no need to start another goroutine.
return return
} }
isstart = true m.isstart = true
registerCommands() registerCommands()
go run() go m.run()
} }
func run() { func(m *taskManager) run() {
now := time.Now().Local() now := time.Now().Local()
for _, t := range AdminTaskList { for _, t := range m.adminTaskList {
t.SetNext(nil, now) t.SetNext(nil, now)
} }
for { for {
// we only use RLock here because NewMapSorter copy the reference, do not change any thing // we only use RLock here because NewMapSorter copy the reference, do not change any thing
taskLock.RLock() m.taskLock.RLock()
sortList := NewMapSorter(AdminTaskList) sortList := NewMapSorter(m.adminTaskList)
taskLock.RUnlock() m.taskLock.RUnlock()
sortList.Sort() sortList.Sort()
var effective time.Time var effective time.Time
if len(AdminTaskList) == 0 || sortList.Vals[0].GetNext(context.Background()).IsZero() { if len(m.adminTaskList) == 0 || sortList.Vals[0].GetNext(context.Background()).IsZero() {
// If there are no entries yet, just sleep - it still handles new entries // If there are no entries yet, just sleep - it still handles new entries
// and stop requests. // and stop requests.
effective = now.AddDate(10, 0, 0) effective = now.AddDate(10, 0, 0)
@ -442,60 +478,64 @@ func run() {
e.SetNext(nil, effective) e.SetNext(nil, effective)
} }
continue continue
case <-changed: case <-m.changed:
now = time.Now().Local() now = time.Now().Local()
taskLock.Lock() m.taskLock.Lock()
for _, t := range AdminTaskList { for _, t := range m.adminTaskList {
t.SetNext(nil, now) t.SetNext(nil, now)
} }
taskLock.Unlock() m.taskLock.Unlock()
continue continue
case <-stop: case <-m.stop:
taskLock.Lock() m.taskLock.Lock()
if isstart { if m.isstart {
isstart = false m.isstart = false
} }
taskLock.Unlock() m.taskLock.Unlock()
return return
} }
} }
} }
// StopTask stop all tasks // StopTask stop all tasks
func StopTask() { func(m *taskManager) StopTask() {
stop <- true go func() {
m.stop <- true
}()
} }
// AddTask add task with name // AddTask add task with name
func AddTask(taskname string, t Tasker) { func (m *taskManager)AddTask(taskname string, t Tasker) {
isChanged := false isChanged := false
taskLock.Lock() m.taskLock.Lock()
t.SetNext(nil, time.Now().Local()) t.SetNext(nil, time.Now().Local())
AdminTaskList[taskname] = t m.adminTaskList[taskname] = t
if isstart { if m.isstart {
isChanged = true isChanged = true
} }
taskLock.Unlock() m.taskLock.Unlock()
if isChanged { if isChanged {
changed <- true go func() {
m.changed <- true
}()
} }
} }
// DeleteTask delete task with name // DeleteTask delete task with name
func DeleteTask(taskname string) { func(m *taskManager) DeleteTask(taskname string) {
isChanged := false isChanged := false
taskLock.Lock() m.taskLock.Lock()
delete(AdminTaskList, taskname) delete(m.adminTaskList, taskname)
if isstart { if m.isstart {
isChanged = true isChanged = true
} }
taskLock.Unlock() m.taskLock.Unlock()
if isChanged { if isChanged {
changed <- true m.changed <- true
} }
} }
@ -648,7 +688,5 @@ func all(r bounds) uint64 {
} }
func init() { func init() {
AdminTaskList = make(map[string]Tasker) globalTaskManager = newTaskManager()
stop = make(chan bool)
changed = make(chan bool)
} }

View File

@ -41,7 +41,8 @@ func TestParse(t *testing.T) {
} }
func TestModifyTaskListAfterRunning(t *testing.T) { func TestModifyTaskListAfterRunning(t *testing.T) {
tk := NewTask("taska", "0/30 * * * * *", func(ctx context.Context) error { m := newTaskManager()
tk := NewTask("taskb", "0/30 * * * * *", func(ctx context.Context) error {
fmt.Println("hello world") fmt.Println("hello world")
return nil return nil
}) })
@ -49,26 +50,32 @@ func TestModifyTaskListAfterRunning(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
AddTask("taska", tk) m.AddTask("taskb", tk)
StartTask() m.StartTask()
DeleteTask("taska") go func() {
AddTask("taska1", tk) m.DeleteTask("taskb")
}()
go func() {
m.AddTask("taskb1", tk)
}()
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
StopTask() m.StopTask()
} }
func TestSpec(t *testing.T) { func TestSpec(t *testing.T) {
m := newTaskManager()
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(2) wg.Add(2)
tk1 := NewTask("tk1", "0 12 * * * *", func(ctx context.Context) error { fmt.Println("tk1"); return nil }) tk1 := NewTask("tk1", "0 12 * * * *", func(ctx context.Context) error { fmt.Println("tk1"); return nil })
tk2 := NewTask("tk2", "0,10,20 * * * * *", func(ctx context.Context) error { fmt.Println("tk2"); wg.Done(); return nil }) tk2 := NewTask("tk2", "0,10,20 * * * * *", func(ctx context.Context) error { fmt.Println("tk2"); wg.Done(); return nil })
tk3 := NewTask("tk3", "0 10 * * * *", func(ctx context.Context) error { fmt.Println("tk3"); wg.Done(); return nil }) tk3 := NewTask("tk3", "0 10 * * * *", func(ctx context.Context) error { fmt.Println("tk3"); wg.Done(); return nil })
AddTask("tk1", tk1) m.AddTask("tk1", tk1)
AddTask("tk2", tk2) m.AddTask("tk2", tk2)
AddTask("tk3", tk3) m.AddTask("tk3", tk3)
StartTask() m.StartTask()
defer StopTask() defer m.StopTask()
select { select {
case <-time.After(200 * time.Second): case <-time.After(200 * time.Second):