fix deadlock about changed sign

This commit is contained in:
Anker Jam 2020-10-05 01:31:27 +08:00
parent 4dc694411f
commit f1cca45d8d
2 changed files with 32 additions and 3 deletions

View File

@ -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
}
}

View File

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