-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
83 lines (63 loc) · 1.34 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package system
import (
"fmt"
"time"
)
type Monitor struct {
interval time.Duration
counter int
pings chan struct{}
ticker *time.Ticker
pingCallback func(counter int)
}
func NewMonitor(seconds int) *Monitor {
fmt.Println("NewMonitor()")
m := &Monitor{
pings: make(chan struct{}),
pingCallback: func(counter int) {
fmt.Println("Sending ping #", counter)
},
interval: time.Second * time.Duration(seconds),
}
// Don't start the monitor unless the seconds interval is specified
if m.interval > 0 {
m.ticker = time.NewTicker(m.interval)
go m.Start()
}
return m
}
func (m *Monitor) SetPingCallback(cb func(counter int)) {
m.pingCallback = cb
}
func (m *Monitor) ping() {
fmt.Println("Ping()")
// Send struct to pings
m.pings <- struct{}{}
}
func (m *Monitor) Start() {
fmt.Println("Start()")
// interrupt := make(chan os.Signal, 1)
// signal.Notify(interrupt, os.Interrupt)
// loop:
for {
select {
case t := <-m.ticker.C:
fmt.Println("Tick at ", t.String())
go m.ping()
case <-m.pings:
m.counter++
m.pingCallback(m.counter)
// case <-interrupt:
// fmt.Println("Interrupt!!!!")
// break loop
}
}
// fmt.Println("Got here!")
}
func (m *Monitor) Stop() {
fmt.Println("Stopping the monitor...")
if m.ticker != nil {
m.ticker.Stop()
}
fmt.Println("monitor stopped!")
}