- 用SyncMapWithTimeout管理tasksch任务

This commit is contained in:
gazebo
2019-08-07 14:55:54 +08:00
parent 8768fe8236
commit f06456cec1
3 changed files with 40 additions and 34 deletions

View File

@@ -9,7 +9,6 @@ import (
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"git.rosy.net.cn/baseapi/platformapi/autonavi" "git.rosy.net.cn/baseapi/platformapi/autonavi"
@@ -27,11 +26,6 @@ var (
skuNamePat *regexp.Regexp skuNamePat *regexp.Regexp
) )
type SyncMapWithTimeout struct {
sync.Map
timers sync.Map
}
type OrderSkuList []*model.OrderSku type OrderSkuList []*model.OrderSku
func (l OrderSkuList) Len() int { 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*([(].*[)])?$`) 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) { func getJxStoreIDFromOrder(order *model.GoodsOrder) (retVal int) {
if order.JxStoreID != 0 { if order.JxStoreID != 0 {
return order.JxStoreID return order.JxStoreID

View File

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

View File

@@ -2,45 +2,44 @@ package tasksch
import ( import (
"sort" "sort"
"sync"
"time" "time"
"git.rosy.net.cn/jx-callback/business/jxutils"
) )
var ( var (
defTaskMan TaskMan defTaskMan TaskMan
defLastHours = 24 defLastHours = 24
maxStoreTime = 48 * time.Hour // 最多存两天时间
) )
type TaskMan struct { type TaskMan struct {
taskList map[string]ITask taskMap jxutils.SyncMapWithTimeout
locker sync.RWMutex
} }
func init() { func init() {
defTaskMan.taskList = make(map[string]ITask)
} }
func (m *TaskMan) GetTasks(taskID string, fromStatus, toStatus int, lastHours int, createdBy string) (taskList TaskList) { 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 { if lastHours == 0 {
lastHours = defLastHours lastHours = defLastHours
} }
lastTime := time.Now().Add(time.Duration(-lastHours) * time.Hour).Unix() 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() status := v.GetStatus()
if !((createdBy != "" && createdBy != v.GetCreatedBy()) || (taskID != "" && taskID != k) || status < fromStatus || status > toStatus || v.GetCreatedAt().Unix() < lastTime) { if !((createdBy != "" && createdBy != v.GetCreatedBy()) || (taskID != "" && taskID != k) || status < fromStatus || status > toStatus || v.GetCreatedAt().Unix() < lastTime) {
taskList = append(taskList, v) taskList = append(taskList, v)
} }
} return true
})
sort.Sort(TaskList(taskList)) sort.Sort(TaskList(taskList))
return taskList return taskList
} }
func (m *TaskMan) ManageTask(task ITask) ITask { func (m *TaskMan) ManageTask(task ITask) ITask {
m.locker.Lock() m.taskMap.StoreWithTimeout(task.GetID(), task, maxStoreTime)
defer m.locker.Unlock()
m.taskList[task.GetID()] = task
return task return task
} }
@@ -53,7 +52,7 @@ func ManageTask(task ITask) ITask {
} }
func IsTaskRunning(taskID string) bool { 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 true
} }
return false return false