1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
scheduler.go 3.73 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-09-19 00:39 . sched option
package scheduler
import (
"gitee.com/h79/goutils/common/algorithm"
"gitee.com/h79/goutils/common/option"
"sync"
"time"
)
type Scheduler struct {
rm sync.Mutex
groups []*groupScheduled
groupMod uint32
}
var (
inst *Scheduler
once sync.Once
)
func Instance(opts ...option.Option) *Scheduler {
once.Do(func() {
inst = newScheduler(20, 2000, opts...)
})
return inst
}
func NewSchedulerCap(groupNum uint32, capacity int, opts ...option.Option) *Scheduler {
return newScheduler(groupNum, capacity, opts...)
}
func NewScheduler(groupNum uint32, opts ...option.Option) *Scheduler {
return newScheduler(groupNum, 5000, opts...)
}
func newScheduler(groupNum uint32, capacity int, opts ...option.Option) *Scheduler {
location := option.Select(TimeLocationOpt, time.Local, opts...)
duration := option.Select(DurationOpt, time.Minute*2, opts...)
groupNum = option.Select(GroupNumOpt, groupNum, opts...)
capacity = option.Select(CapacityOpt, capacity, opts...)
loggerEnabled := option.Select(LoggEnableOpt, false, opts...)
if groupNum == 0 {
groupNum = 20
}
if groupNum > 2000 {
groupNum = 2000
}
var groups = make([]*groupScheduled, groupNum)
for i := 0; i < int(groupNum); i++ {
groups[i] = newGroupScheduled(i, capacity, location, duration, loggerEnabled)
}
return &Scheduler{
groupMod: groupNum,
groups: groups,
}
}
func (s *Scheduler) groupScheduled(group uint32) *groupScheduled {
s.rm.Lock()
defer s.rm.Unlock()
return s.groups[group]
}
func (s *Scheduler) GroupIndex(key string) uint32 {
return algorithm.HashCode(key) % s.groupMod
}
func (s *Scheduler) Index(id uint32) uint32 {
return id % s.groupMod
}
func (s *Scheduler) AddTask(task Task, opts ...option.Option) error {
return s.AddGTask(s.GroupIndex(task.GetId()), task, opts...)
}
func (s *Scheduler) AddGTask(group uint32, task Task, opts ...option.Option) error {
groups := s.groupScheduled(group)
return groups.AddTask(task, opts...)
}
func (s *Scheduler) AddTriggerTask(task Task, trigger Trigger, opts ...option.Option) error {
return s.AddTriggerGTask(s.GroupIndex(task.GetId()), task, trigger, opts...)
}
func (s *Scheduler) AddTriggerGTask(group uint32, task Task, trigger Trigger, opts ...option.Option) error {
groups := s.groupScheduled(group)
return groups.AddTriggerTask(task, trigger, opts...)
}
func (s *Scheduler) UpdateTask(taskId string, opts ...option.Option) bool {
return s.UpdateGTask(s.GroupIndex(taskId), taskId, opts...)
}
func (s *Scheduler) UpdateGTask(group uint32, taskId string, opts ...option.Option) bool {
groups := s.groupScheduled(group)
return groups.UpdateTask(taskId, opts...)
}
func (s *Scheduler) ForeachTask(group uint32, update func(group int, task Task, time time.Duration, now, trigger int64) bool) {
groups := s.groupScheduled(group)
groups.ForeachTask(update)
}
func (s *Scheduler) Foreach(update func(group int, task Task, time time.Duration, now, trigger int64) bool) {
for i := 0; i < len(s.groups); i++ {
s.groups[i].ForeachTask(update)
}
}
func (s *Scheduler) StopTask(taskId string) {
s.StopGTask(s.GroupIndex(taskId), taskId)
}
func (s *Scheduler) StopGTask(group uint32, taskId string) {
groups := s.groupScheduled(group)
groups.StopTask(taskId)
}
func (s *Scheduler) RemoveTask(taskId string) {
s.RemoveGTask(s.GroupIndex(taskId), taskId)
}
func (s *Scheduler) RemoveGTask(group uint32, taskId string) {
groups := s.groupScheduled(group)
groups.RemoveTask(taskId)
}
func (s *Scheduler) HasTask(taskId string) bool {
return s.HasGTask(s.GroupIndex(taskId), taskId)
}
func (s *Scheduler) HasGTask(group uint32, taskId string) bool {
groups := s.groupScheduled(group)
return groups.HasTask(taskId)
}
func (s *Scheduler) Run() {
s.rm.Lock()
defer s.rm.Unlock()
for i := range s.groups {
s.groups[i].Run()
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.22.4

搜索帮助