mirror of
https://github.com/astaxie/beego.git
synced 2024-11-24 09:10:53 +00:00
fix deadlock about changed sign
This commit is contained in:
parent
4dc694411f
commit
f1cca45d8d
@ -468,21 +468,33 @@ func StopTask() {
|
||||
|
||||
// AddTask add task with name
|
||||
func AddTask(taskname string, t Tasker) {
|
||||
isChanged := false
|
||||
taskLock.Lock()
|
||||
defer taskLock.Unlock()
|
||||
t.SetNext(nil, time.Now().Local())
|
||||
AdminTaskList[taskname] = t
|
||||
if isstart {
|
||||
isChanged = true
|
||||
}
|
||||
taskLock.Unlock()
|
||||
|
||||
if isChanged {
|
||||
changed <- true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// DeleteTask delete task with name
|
||||
func DeleteTask(taskname string) {
|
||||
isChanged := false
|
||||
|
||||
taskLock.Lock()
|
||||
defer taskLock.Unlock()
|
||||
delete(AdminTaskList, taskname)
|
||||
if isstart {
|
||||
isChanged = true
|
||||
}
|
||||
taskLock.Unlock()
|
||||
|
||||
if isChanged {
|
||||
changed <- true
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,24 @@ func TestParse(t *testing.T) {
|
||||
}
|
||||
AddTask("taska", tk)
|
||||
StartTask()
|
||||
time.Sleep(6 * time.Second)
|
||||
time.Sleep(3 * time.Second)
|
||||
StopTask()
|
||||
}
|
||||
|
||||
func TestModifyTaskListAfterRunning(t *testing.T) {
|
||||
tk := NewTask("taska", "0/30 * * * * *", func(ctx context.Context) error {
|
||||
fmt.Println("hello world")
|
||||
return nil
|
||||
})
|
||||
err := tk.Run(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
AddTask("taska", tk)
|
||||
StartTask()
|
||||
DeleteTask("taska")
|
||||
AddTask("taska1", tk)
|
||||
time.Sleep(3 * time.Second)
|
||||
StopTask()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user