40 Star 146 Fork 3

Gitee 极速下载/grafana

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/grafana/grafana
克隆/下载
scheduler.go 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
Karsten Weiss 提交于 2018-04-16 20:22 . Simplify make() (gosimple)
package alerting
import (
"math"
"time"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
)
type SchedulerImpl struct {
jobs map[int64]*Job
log log.Logger
}
func NewScheduler() Scheduler {
return &SchedulerImpl{
jobs: make(map[int64]*Job),
log: log.New("alerting.scheduler"),
}
}
func (s *SchedulerImpl) Update(rules []*Rule) {
s.log.Debug("Scheduling update", "ruleCount", len(rules))
jobs := make(map[int64]*Job)
for i, rule := range rules {
var job *Job
if s.jobs[rule.Id] != nil {
job = s.jobs[rule.Id]
} else {
job = &Job{
Running: false,
}
}
job.Rule = rule
offset := ((rule.Frequency * 1000) / int64(len(rules))) * int64(i)
job.Offset = int64(math.Floor(float64(offset) / 1000))
if job.Offset == 0 { //zero offset causes division with 0 panics.
job.Offset = 1
}
jobs[rule.Id] = job
}
s.jobs = jobs
}
func (s *SchedulerImpl) Tick(tickTime time.Time, execQueue chan *Job) {
now := tickTime.Unix()
for _, job := range s.jobs {
if job.Running || job.Rule.State == models.AlertStatePaused {
continue
}
if job.OffsetWait && now%job.Offset == 0 {
job.OffsetWait = false
s.enqueue(job, execQueue)
continue
}
if now%job.Rule.Frequency == 0 {
if job.Offset > 0 {
job.OffsetWait = true
} else {
s.enqueue(job, execQueue)
}
}
}
}
func (s *SchedulerImpl) enqueue(job *Job, execQueue chan *Job) {
s.log.Debug("Scheduler: Putting job on to exec queue", "name", job.Rule.Name, "id", job.Rule.Id)
execQueue <- job
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/grafana.git
git@gitee.com:mirrors/grafana.git
mirrors
grafana
grafana
v6.0.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385