mirror of
https://github.com/astaxie/beego.git
synced 2024-11-24 12:00:54 +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
|
// AddTask add task with name
|
||||||
func AddTask(taskname string, t Tasker) {
|
func AddTask(taskname string, t Tasker) {
|
||||||
|
isChanged := false
|
||||||
taskLock.Lock()
|
taskLock.Lock()
|
||||||
defer taskLock.Unlock()
|
|
||||||
t.SetNext(nil, time.Now().Local())
|
t.SetNext(nil, time.Now().Local())
|
||||||
AdminTaskList[taskname] = t
|
AdminTaskList[taskname] = t
|
||||||
if isstart {
|
if isstart {
|
||||||
|
isChanged = true
|
||||||
|
}
|
||||||
|
taskLock.Unlock()
|
||||||
|
|
||||||
|
if isChanged {
|
||||||
changed <- true
|
changed <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteTask delete task with name
|
// DeleteTask delete task with name
|
||||||
func DeleteTask(taskname string) {
|
func DeleteTask(taskname string) {
|
||||||
|
isChanged := false
|
||||||
|
|
||||||
taskLock.Lock()
|
taskLock.Lock()
|
||||||
defer taskLock.Unlock()
|
|
||||||
delete(AdminTaskList, taskname)
|
delete(AdminTaskList, taskname)
|
||||||
if isstart {
|
if isstart {
|
||||||
|
isChanged = true
|
||||||
|
}
|
||||||
|
taskLock.Unlock()
|
||||||
|
|
||||||
|
if isChanged {
|
||||||
changed <- true
|
changed <- true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,24 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
AddTask("taska", tk)
|
AddTask("taska", tk)
|
||||||
StartTask()
|
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()
|
StopTask()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user