代码拉取完成,页面将自动刷新
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 WithLoggerEnabled() option.Option {
return option.WithOpt(true, "", 4)
}
func WithTaskDuration(t time.Duration) option.Option {
return option.WithOpt(t, "", 1)
}
func WithGroupNum(num int) option.Option {
return option.WithOpt(num, "", 2)
}
func WithCapacity(capacity int) option.Option {
return option.WithOpt(capacity, "", 3)
}
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 {
duration := option.Select(1, time.Millisecond*10, opts...)
groupNum = option.Select(2, groupNum, opts...)
capacity = option.Select(3, capacity, opts...)
loggerEnabled := option.Select(4, false, opts...)
if groupNum == 0 {
groupNum = 20
}
if groupNum > 2000 {
groupNum = 2000
}
var i uint32
var groups = make([]*groupScheduled, groupNum)
for i = 0; i < groupNum; i++ {
groups[i] = newGroupScheduled(capacity, 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) {
s.AddGTask(s.GroupIndex(task.GetId()), task)
}
func (s *Scheduler) AddGTask(group uint32, task Task) {
groups := s.groupScheduled(group)
groups.AddTask(task)
}
func (s *Scheduler) AddTriggerTask(task Task, trigger Trigger) error {
return s.AddTriggerGTask(s.GroupIndex(task.GetId()), task, trigger)
}
func (s *Scheduler) AddTriggerGTask(group uint32, task Task, trigger Trigger) error {
groups := s.groupScheduled(group)
return groups.AddTriggerTask(task, trigger)
}
func (s *Scheduler) UpdateTask(taskId string, update func(task Task) bool) bool {
return s.UpdateGTask(s.GroupIndex(taskId), taskId, update)
}
func (s *Scheduler) UpdateGTask(group uint32, taskId string, update func(task Task) bool) bool {
groups := s.groupScheduled(group)
return groups.UpdateTask(taskId, 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()
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。