grafana/pkg/services/alerting/scheduler.go

79 lines
1.5 KiB
Go
Raw Normal View History

package alerting
import (
"math"
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
)
type schedulerImpl struct {
2016-07-27 09:29:28 -05:00
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)
2016-07-27 09:29:28 -05:00
for i, rule := range rules {
var job *Job
if s.jobs[rule.ID] != nil {
job = s.jobs[rule.ID]
} else {
2016-07-27 09:29:28 -05:00
job = &Job{
Running: false,
}
}
2016-06-06 07:24:14 -05:00
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
2018-04-13 11:40:14 -05:00
s.enqueue(job, execQueue)
continue
}
if now%job.Rule.Frequency == 0 {
if job.Offset > 0 {
job.OffsetWait = true
} else {
2018-04-13 11:40:14 -05:00
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
}