/
repmon.go
71 lines (58 loc) · 1.37 KB
/
repmon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package repmon
import (
"time"
log "github.com/sirupsen/logrus"
)
// RepMon monitors a database to ensure that replication is running.
type RepMon struct {
config *Config
replicationChecker ReplicationChecker
notifier Notifier
running bool
stopc chan struct{}
donec chan struct{}
}
// New returns a new RepMon instance.
func New(config *Config, replicationChecker ReplicationChecker, notifier Notifier) *RepMon {
return &RepMon{
config: config,
replicationChecker: replicationChecker,
notifier: notifier,
stopc: make(chan struct{}),
donec: make(chan struct{}),
}
}
// Start starts monitoring the configured database.
func (r *RepMon) Start() {
if r.running {
return
}
r.running = true
go r.loop()
}
// Stop stops monitoring the configured database.
func (r *RepMon) Stop() {
if !r.running {
return
}
r.stopc <- struct{}{}
<-r.donec
r.running = false
}
func (r *RepMon) loop() {
ticker := time.NewTicker(r.config.frequency)
log.Infof("RepMon started: checking slave replication every %s", r.config.Frequency)
for {
select {
case <-ticker.C:
if err := r.replicationChecker.Replicating(); err != nil {
r.notifier.Notify(err)
}
case <-r.stopc:
ticker.Stop()
r.donec <- struct{}{}
log.Info("RepMon shutdown")
return
}
}
}