diff --git a/business/jxutils/jxutils.go b/business/jxutils/jxutils.go index 19d56eebb..6cf88c210 100644 --- a/business/jxutils/jxutils.go +++ b/business/jxutils/jxutils.go @@ -9,7 +9,6 @@ import ( "regexp" "sort" "strings" - "sync" "time" "git.rosy.net.cn/baseapi/platformapi/autonavi" @@ -27,11 +26,6 @@ var ( skuNamePat *regexp.Regexp ) -type SyncMapWithTimeout struct { - sync.Map - timers sync.Map -} - type OrderSkuList []*model.OrderSku func (l OrderSkuList) Len() int { @@ -60,22 +54,6 @@ func init() { skuNamePat = regexp.MustCompile(`([\((\[【][^\((\[【\))\]】]*[\))\]】])?(.*?)([((].*[))])?\s*约?([1-9][\d\.]*)(g|G|kg|kG|Kg|KG|l|L|ml|mL|Ml|ML|克)\s*([((].*[))])?\s*(?:\/|/|)\s*([^\s()()]{0,2})\s*([((].*[))])?$`) } -func (m *SyncMapWithTimeout) StoreWithTimeout(key, value interface{}, timeout time.Duration) { - m.Map.Store(key, value) - m.timers.Store(key, utils.AfterFuncWithRecover(timeout, func() { - m.Delete(key) - })) -} - -func (m *SyncMapWithTimeout) Delete(key interface{}) { - m.Map.Delete(key) - if value, ok := m.timers.Load(key); ok { - timer := value.(*time.Timer) - timer.Stop() - } - m.timers.Delete(key) -} - func getJxStoreIDFromOrder(order *model.GoodsOrder) (retVal int) { if order.JxStoreID != 0 { return order.JxStoreID diff --git a/business/jxutils/jxutils_sync_map.go b/business/jxutils/jxutils_sync_map.go new file mode 100644 index 000000000..92827aea6 --- /dev/null +++ b/business/jxutils/jxutils_sync_map.go @@ -0,0 +1,29 @@ +package jxutils + +import ( + "sync" + "time" + + "git.rosy.net.cn/baseapi/utils" +) + +type SyncMapWithTimeout struct { + sync.Map + timers sync.Map +} + +func (m *SyncMapWithTimeout) StoreWithTimeout(key, value interface{}, timeout time.Duration) { + m.Map.Store(key, value) + m.timers.Store(key, utils.AfterFuncWithRecover(timeout, func() { + m.Delete(key) + })) +} + +func (m *SyncMapWithTimeout) Delete(key interface{}) { + m.Map.Delete(key) + if value, ok := m.timers.Load(key); ok { + timer := value.(*time.Timer) + timer.Stop() + } + m.timers.Delete(key) +} diff --git a/business/jxutils/tasksch/task_man.go b/business/jxutils/tasksch/task_man.go index 4dd270358..556e85ea2 100644 --- a/business/jxutils/tasksch/task_man.go +++ b/business/jxutils/tasksch/task_man.go @@ -2,45 +2,44 @@ package tasksch import ( "sort" - "sync" "time" + + "git.rosy.net.cn/jx-callback/business/jxutils" ) var ( defTaskMan TaskMan defLastHours = 24 + maxStoreTime = 48 * time.Hour // 最多存两天时间 ) type TaskMan struct { - taskList map[string]ITask - locker sync.RWMutex + taskMap jxutils.SyncMapWithTimeout } func init() { - defTaskMan.taskList = make(map[string]ITask) } func (m *TaskMan) GetTasks(taskID string, fromStatus, toStatus int, lastHours int, createdBy string) (taskList TaskList) { - m.locker.RLock() - defer m.locker.RUnlock() if lastHours == 0 { lastHours = defLastHours } lastTime := time.Now().Add(time.Duration(-lastHours) * time.Hour).Unix() - for k, v := range m.taskList { + m.taskMap.Range(func(key, value interface{}) bool { + k := key.(string) + v := value.(ITask) status := v.GetStatus() if !((createdBy != "" && createdBy != v.GetCreatedBy()) || (taskID != "" && taskID != k) || status < fromStatus || status > toStatus || v.GetCreatedAt().Unix() < lastTime) { taskList = append(taskList, v) } - } + return true + }) sort.Sort(TaskList(taskList)) return taskList } func (m *TaskMan) ManageTask(task ITask) ITask { - m.locker.Lock() - defer m.locker.Unlock() - m.taskList[task.GetID()] = task + m.taskMap.StoreWithTimeout(task.GetID(), task, maxStoreTime) return task } @@ -53,7 +52,7 @@ func ManageTask(task ITask) ITask { } func IsTaskRunning(taskID string) bool { - if taskList := GetTasks(taskID, TaskStatusBegin, TaskStatusWorking, 36, ""); len(taskList) > 0 { + if taskList := GetTasks(taskID, TaskStatusBegin, TaskStatusWorking, 0, ""); len(taskList) > 0 { return true } return false