-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.go
67 lines (51 loc) · 1.27 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
package main
import "time"
type Starter func()
type Runner func()
type Monitor struct {
reset chan int
longInterval time.Duration
shortInterval time.Duration
shortTimer *time.Timer
shortDuration time.Duration
starter Starter
runner Runner
}
func CreateMonitor(starter Starter, runner Runner, longInterval time.Duration, shortInterval time.Duration, shortDuration time.Duration) *Monitor {
monitor := &Monitor{
reset: make(chan int),
longInterval: longInterval,
shortInterval: shortInterval,
shortDuration: shortDuration,
starter: starter,
runner: runner,
}
go monitor.run()
return monitor
}
func (monitor *Monitor) Reset() {
monitor.reset <- 0
}
func (monitor *Monitor) run() {
monitor.starter()
monitor.runner()
ticker := time.NewTicker(monitor.shortInterval)
monitor.shortTimer = time.NewTimer(monitor.shortDuration)
for {
select {
case <-ticker.C:
monitor.runner()
case <-monitor.reset:
ticker.Stop()
monitor.shortTimer.Stop()
monitor.starter()
monitor.runner()
ticker = time.NewTicker(monitor.shortInterval)
monitor.shortTimer = time.NewTimer(monitor.shortDuration)
case <-monitor.shortTimer.C:
ticker.Stop()
monitor.shortTimer.Stop()
ticker = time.NewTicker(monitor.longInterval)
}
}
}