1 Star 4 Fork 0

magicianlyx/GoTask

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
taskmap.go 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
magicianlyx 提交于 2021-11-20 21:44 +08:00 . -
package GoTask
import (
"sync"
"time"
)
// 任务字典 线程安全
type TaskMap struct {
tMap sync.Map
}
func NewTaskMap() *TaskMap {
return &TaskMap{
tMap: sync.Map{},
}
}
// 添加
func (tm *TaskMap) Add(key string, task *TaskInfo) {
if !tm.IsExist(key) {
tm.tMap.Store(key, task)
}
}
// 存在时才修改
func (tm *TaskMap) Set(key string, task *TaskInfo) {
if tm.IsExist(key) {
tm.tMap.Store(key, task)
}
}
// 添加或修改
func (tm *TaskMap) AddOrSet(key string, task *TaskInfo) {
tm.tMap.Store(key, task)
}
// 删除
func (tm *TaskMap) Delete(key string) {
tm.tMap.Delete(key)
}
// 获取 返回副本
func (tm *TaskMap) Get(key string) *TaskInfo {
if v, ok := tm.tMap.Load(key); ok {
if v1, ok1 := v.(*TaskInfo); ok1 {
return v1.Clone()
}
}
return nil
}
// 键是否存在
func (tm *TaskMap) IsExist(key string) bool {
_, ok := tm.tMap.Load(key)
return ok
}
// 选择下一个最早执行的任务
func (tm *TaskMap) SelectNextExec() (*TaskInfo, time.Duration, bool) {
var minv *TaskInfo
tm.tMap.Range(func(key, value interface{}) bool {
v, ok := value.(*TaskInfo)
if !ok {
return true
}
if !v.HasNextExecute() {
return true
}
if minv == nil {
minv = v
return true
}
mnt := minv.NextScheduleTime()
vnt := v.NextScheduleTime()
if mnt.UnixNano() > vnt.UnixNano() {
minv = v
}
return true
})
if minv == nil {
return nil, 0, false
}
ns := minv.NextScheduleTime()
spec := ns.Sub(time.Now())
if spec <= 0 {
spec = time.Nanosecond
}
return minv, spec, true
}
// 获取所有返回副本
func (tm *TaskMap) GetAll() map[string]*TaskInfo {
m := map[string]*TaskInfo{}
tm.tMap.Range(func(key, value interface{}) bool {
k, ok := key.(string)
if !ok {
return true
}
v, ok := value.(*TaskInfo)
if !ok {
return true
}
m[k] = v.Clone()
return true
})
return m
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/magicianlyx/GoTask.git
git@gitee.com:magicianlyx/GoTask.git
magicianlyx
GoTask
GoTask
v1.0.15

搜索帮助