From cb21bf41b0cbc30b6bb8169285807a2163c0f5ac Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 23 May 2016 12:15:36 +0200 Subject: [PATCH] tech(alerting): split code into different files --- pkg/services/alerting/alert_rule_reader.gi.go | 22 +++++++++++ pkg/services/alerting/alerting.go | 37 +------------------ pkg/services/alerting/executor.go | 22 +++++++++++ 3 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 pkg/services/alerting/alert_rule_reader.gi.go create mode 100644 pkg/services/alerting/executor.go diff --git a/pkg/services/alerting/alert_rule_reader.gi.go b/pkg/services/alerting/alert_rule_reader.gi.go new file mode 100644 index 00000000000..a6314c64eba --- /dev/null +++ b/pkg/services/alerting/alert_rule_reader.gi.go @@ -0,0 +1,22 @@ +package alerting + +import ( + m "github.com/grafana/grafana/pkg/models" +) + +type RuleReader interface { + Fetch() []m.AlertRule +} + +type AlertRuleReader struct{} + +func (this AlertRuleReader) Fetch() []m.AlertRule { + return []m.AlertRule{ + {Id: 1, Title: "alert rule 1", Interval: "10s", Frequency: 10}, + {Id: 2, Title: "alert rule 2", Interval: "10s", Frequency: 10}, + {Id: 3, Title: "alert rule 3", Interval: "10s", Frequency: 10}, + {Id: 4, Title: "alert rule 4", Interval: "10s", Frequency: 5}, + {Id: 5, Title: "alert rule 5", Interval: "10s", Frequency: 5}, + {Id: 6, Title: "alert rule 6", Interval: "10s", Frequency: 1}, + } +} diff --git a/pkg/services/alerting/alerting.go b/pkg/services/alerting/alerting.go index 23bd5733298..ae44a6dd557 100644 --- a/pkg/services/alerting/alerting.go +++ b/pkg/services/alerting/alerting.go @@ -53,7 +53,7 @@ func (s *Scheduler) heartBeat() { func (s *Scheduler) Dispatch(reader RuleReader) { reschedule := time.NewTicker(time.Second * 10) secondTicker := time.NewTicker(time.Second) - ticker := time.NewTicker(time.Second * 5) + heartbeat := time.NewTicker(time.Second * 5) s.heartBeat() s.updateJobs(reader.Fetch) @@ -64,7 +64,7 @@ func (s *Scheduler) Dispatch(reader RuleReader) { s.queueJobs() case <-reschedule.C: s.updateJobs(reader.Fetch) - case <-ticker.C: + case <-heartbeat.C: s.heartBeat() } } @@ -104,7 +104,6 @@ func (s *Scheduler) queueJobs() { } func (s *Scheduler) Executor(executor Executor) { - for job := range s.runQueue { log.Info("Executor: queue length %d", len(s.runQueue)) log.Info("Executor: executing %s", job.name) @@ -126,35 +125,3 @@ type AlertResult struct { state string duration time.Time } - -type RuleReader interface { - Fetch() []m.AlertRule -} - -type AlertRuleReader struct{} - -func (this AlertRuleReader) Fetch() []m.AlertRule { - return []m.AlertRule{ - {Id: 1, Title: "alert rule 1", Interval: "10s", Frequency: 10}, - {Id: 2, Title: "alert rule 2", Interval: "10s", Frequency: 10}, - {Id: 3, Title: "alert rule 3", Interval: "10s", Frequency: 10}, - {Id: 4, Title: "alert rule 4", Interval: "10s", Frequency: 5}, - {Id: 5, Title: "alert rule 5", Interval: "10s", Frequency: 5}, - {Id: 6, Title: "alert rule 6", Interval: "10s", Frequency: 1}, - } -} - -type Executor interface { - Execute(rule m.AlertRule) (err error, result AlertResult) -} - -type DummieExecutor struct{} - -func (this DummieExecutor) Execute(rule m.AlertRule) (err error, result AlertResult) { - if rule.Id == 6 { - time.Sleep(time.Second * 60) - } - time.Sleep(time.Second) - log.Info("Finnished executing: %d", rule.Id) - return nil, AlertResult{state: "OK", id: rule.Id} -} diff --git a/pkg/services/alerting/executor.go b/pkg/services/alerting/executor.go new file mode 100644 index 00000000000..fcde8be8865 --- /dev/null +++ b/pkg/services/alerting/executor.go @@ -0,0 +1,22 @@ +package alerting + +import ( + "github.com/grafana/grafana/pkg/log" + m "github.com/grafana/grafana/pkg/models" + "time" +) + +type Executor interface { + Execute(rule m.AlertRule) (err error, result AlertResult) +} + +type DummieExecutor struct{} + +func (this DummieExecutor) Execute(rule m.AlertRule) (err error, result AlertResult) { + if rule.Id == 6 { + time.Sleep(time.Second * 60) + } + time.Sleep(time.Second) + log.Info("Finnished executing: %d", rule.Id) + return nil, AlertResult{state: "OK", id: rule.Id} +}