- fix concurrent access of TaskMan

This commit is contained in:
gazebo
2019-01-02 17:37:38 +08:00
parent 5508b42835
commit 12a3b4265d

View File

@@ -2,6 +2,7 @@ package tasksch
import ( import (
"sort" "sort"
"sync"
"time" "time"
) )
@@ -12,6 +13,7 @@ var (
type TaskMan struct { type TaskMan struct {
taskList map[string]ITask taskList map[string]ITask
locker sync.RWMutex
} }
func init() { func init() {
@@ -19,6 +21,8 @@ func init() {
} }
func (m *TaskMan) GetTasks(taskID string, fromStatus, toStatus int, lastHours int) (taskList TaskList) { func (m *TaskMan) GetTasks(taskID string, fromStatus, toStatus int, lastHours int) (taskList TaskList) {
m.locker.RLock()
defer m.locker.RUnlock()
if lastHours == 0 { if lastHours == 0 {
lastHours = defLastHours lastHours = defLastHours
} }
@@ -34,6 +38,8 @@ func (m *TaskMan) GetTasks(taskID string, fromStatus, toStatus int, lastHours in
} }
func (m *TaskMan) ManageTask(task ITask) ITask { func (m *TaskMan) ManageTask(task ITask) ITask {
m.locker.Lock()
defer m.locker.Unlock()
m.taskList[task.GetID()] = task m.taskList[task.GetID()] = task
return task return task
} }