-
Notifications
You must be signed in to change notification settings - Fork 210
/
monitor.go
51 lines (44 loc) · 961 Bytes
/
monitor.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
package monitoring
import (
"time"
)
type recorder interface {
Update()
Status() string
LogJSON()
}
// Monitor is a simple service to take samples
type Monitor struct {
updateTicker *time.Ticker
printTicker *time.Ticker
recorder recorder
term chan struct{}
}
// NewMonitor constructs a new monitor instance
func NewMonitor(updateRate time.Duration, printRate time.Duration, updater recorder, termChannel chan struct{}) *Monitor {
m := new(Monitor)
m.updateTicker = time.NewTicker(updateRate)
m.printTicker = time.NewTicker(printRate)
m.recorder = updater
m.term = termChannel
return m
}
func (m *Monitor) monitor() {
for {
select {
case <-m.term:
m.updateTicker.Stop()
m.printTicker.Stop()
return
case <-m.updateTicker.C:
m.recorder.Update()
case <-m.printTicker.C:
//log.Info("%v", m.recorder.Status())
m.recorder.LogJSON()
}
}
}
// Start monitoring
func (m *Monitor) Start() {
go m.monitor()
}