Fix rule manager shutdown.

This commit is contained in:
Bjoern Rabenstein 2015-01-29 15:05:10 +01:00
parent 3948e2a7f8
commit 26e22e6ad6
1 changed files with 15 additions and 11 deletions

View File

@ -128,26 +128,30 @@ func NewRuleManager(o *RuleManagerOptions) RuleManager {
}
func (m *ruleManager) Run() {
defer glog.Info("Rule manager stopped.")
ticker := time.NewTicker(m.interval)
defer ticker.Stop()
for {
// TODO(beorn): This has the same problem as the scraper had
// before. If rule evaluation takes longer than the interval,
// there is a 50% chance per iteration that - after stopping the
// ruleManager - a new evaluation will be started rather than
// the ruleManager actually stopped. We need a similar
// contraption here as in the scraper.
// The outer select clause makes sure that m.done is looked at
// first. Otherwise, if m.runIteration takes longer than
// m.interval, there is only a 50% chance that m.done will be
// looked at before the next m.runIteration call happens.
select {
case <-m.done:
return
default:
select {
case <-ticker.C:
start := time.Now()
m.runIteration(m.results)
iterationDuration.Observe(float64(time.Since(start) / time.Millisecond))
case <-m.done:
glog.Info("Rule manager stopped.")
return
}
}
}
}
func (m *ruleManager) Stop() {