forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
105 lines (87 loc) · 2.4 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package control
import "time"
const (
MonitorStopped monitorState = iota - 1 // default is stopped
MonitorStarted
// Changed to one second until we get proper control of duration runtime into this.
DefaultMonitorDuration = time.Second * 1
)
type monitorState int
type monitor struct {
State monitorState
duration time.Duration
quit chan struct{}
}
type monitorOption func(m *monitor) monitorOption
// Option sets the options specified.
// Returns an option to optionally restore the last arg's previous value.
func (m *monitor) Option(opts ...monitorOption) monitorOption {
var previous monitorOption
for _, opt := range opts {
previous = opt(m)
}
return previous
}
// MonitorDuration sets monitor's duration to v.
func MonitorDurationOption(v time.Duration) monitorOption {
return func(m *monitor) monitorOption {
previous := m.duration
m.duration = v
return MonitorDurationOption(previous)
}
}
func newMonitor(opts ...monitorOption) *monitor {
mon := &monitor{
State: MonitorStopped,
duration: DefaultMonitorDuration,
}
//set options
for _, opt := range opts {
opt(mon)
}
return mon
}
// start the monitor
func (m *monitor) Start(availablePlugins *availablePlugins) {
//start a routine that will be fired every X duration looping
//over available plugins and firing a health check routine
ticker := time.NewTicker(m.duration)
m.quit = make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
go func() {
availablePlugins.RLock()
for _, ap := range availablePlugins.all() {
go ap.CheckHealth()
}
availablePlugins.RUnlock()
}()
case <-m.quit:
ticker.Stop()
m.State = MonitorStopped
return
}
}
}()
m.State = MonitorStarted
}
// stop the monitor
func (m *monitor) Stop() {
close(m.quit)
m.State = MonitorStopped
}