2016-06-06 03:31:21 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2016-06-06 04:56:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-06-06 03:31:21 -05:00
|
|
|
)
|
|
|
|
|
2016-06-06 04:56:58 -05:00
|
|
|
type SchedulerImpl struct {
|
|
|
|
jobs map[int64]*AlertJob
|
2016-06-10 03:00:00 -05:00
|
|
|
log log.Logger
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 04:56:58 -05:00
|
|
|
func NewScheduler() Scheduler {
|
|
|
|
return &SchedulerImpl{
|
|
|
|
jobs: make(map[int64]*AlertJob, 0),
|
2016-06-10 03:00:00 -05:00
|
|
|
log: log.New("alerting.scheduler"),
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 06:49:11 -05:00
|
|
|
func (s *SchedulerImpl) Update(alerts []*AlertRule) {
|
|
|
|
s.log.Debug("Scheduling update", "alerts.count", len(alerts))
|
2016-06-06 03:31:21 -05:00
|
|
|
|
|
|
|
jobs := make(map[int64]*AlertJob, 0)
|
|
|
|
|
2016-06-11 06:49:11 -05:00
|
|
|
for i, rule := range alerts {
|
2016-06-06 03:31:21 -05:00
|
|
|
var job *AlertJob
|
2016-06-06 06:50:47 -05:00
|
|
|
if s.jobs[rule.Id] != nil {
|
|
|
|
job = s.jobs[rule.Id]
|
2016-06-06 03:31:21 -05:00
|
|
|
} else {
|
|
|
|
job = &AlertJob{
|
2016-07-27 05:09:55 -05:00
|
|
|
Running: false,
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 07:24:14 -05:00
|
|
|
job.Rule = rule
|
2016-06-06 03:31:21 -05:00
|
|
|
job.Offset = int64(i)
|
|
|
|
|
|
|
|
jobs[rule.Id] = job
|
|
|
|
}
|
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
s.jobs = jobs
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
func (s *SchedulerImpl) Tick(tickTime time.Time, execQueue chan *AlertJob) {
|
2016-06-06 04:56:58 -05:00
|
|
|
now := tickTime.Unix()
|
|
|
|
|
2016-06-06 06:50:47 -05:00
|
|
|
for _, job := range s.jobs {
|
2016-06-06 03:31:21 -05:00
|
|
|
if now%job.Rule.Frequency == 0 && job.Running == false {
|
2016-06-10 04:37:03 -05:00
|
|
|
s.log.Debug("Scheduler: Putting job on to exec queue", "name", job.Rule.Name)
|
2016-06-06 04:56:58 -05:00
|
|
|
execQueue <- job
|
2016-06-06 03:31:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|