-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
132 lines (119 loc) · 3.33 KB
/
scanner.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cosmovisor
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"time"
upgradetypes "github.com/mycodeku/transtionhelper/x/upgrade/types"
)
type fileWatcher struct {
// full path to a watched file
filename string
interval time.Duration
currentInfo upgradetypes.Plan
lastModTime time.Time
cancel chan bool
ticker *time.Ticker
needsUpdate bool
initialized bool
}
func newUpgradeFileWatcher(filename string, interval time.Duration) (*fileWatcher, error) {
if filename == "" {
return nil, errors.New("filename undefined")
}
filenameAbs, err := filepath.Abs(filename)
if err != nil {
return nil,
fmt.Errorf("wrong path, %s must be a valid file path, [%w]", filename, err)
}
dirname := filepath.Dir(filename)
info, err := os.Stat(dirname)
if err != nil || !info.IsDir() {
return nil, fmt.Errorf("wrong path, %s must be an existing directory, [%w]", dirname, err)
}
return &fileWatcher{filenameAbs, interval, upgradetypes.Plan{}, time.Time{}, make(chan bool), time.NewTicker(interval), false, false}, nil
}
func (fw *fileWatcher) Stop() {
close(fw.cancel)
}
// pools the filesystem to check for new upgrade currentInfo. currentName is the name
// of currently running upgrade. The check is rejected if it finds an upgrade with the same
// name.
func (fw *fileWatcher) MonitorUpdate(currentUpgrade upgradetypes.Plan) <-chan struct{} {
fw.ticker.Reset(fw.interval)
done := make(chan struct{})
fw.cancel = make(chan bool)
fw.needsUpdate = false
go func() {
for {
select {
case <-fw.ticker.C:
if fw.CheckUpdate(currentUpgrade) {
done <- struct{}{}
return
}
case <-fw.cancel:
return
}
}
}()
return done
}
// CheckUpdate reads update plan from file and checks if there is a new update request
// currentName is the name of currently running upgrade. The check is rejected if it finds
// an upgrade with the same name.
func (fw *fileWatcher) CheckUpdate(currentUpgrade upgradetypes.Plan) bool {
if fw.needsUpdate {
return true
}
stat, err := os.Stat(fw.filename)
if err != nil { // file doesn't exists
return false
}
if !stat.ModTime().After(fw.lastModTime) {
return false
}
info, err := parseUpgradeInfoFile(fw.filename)
if err != nil {
Logger.Fatal().Err(err).Msg("Can't parse upgrade info file")
return false
}
if !fw.initialized { // daemon has restarted
fw.initialized = true
fw.currentInfo = info
fw.lastModTime = stat.ModTime()
// heuristic: deamon has restarted, so we don't know if we successfully downloaded the upgrade or not.
// so we try to compare the running upgrade name (read from the cosmovisor file) with the upgrade info
if currentUpgrade.Name != fw.currentInfo.Name {
fw.needsUpdate = true
return true
}
}
if info.Height > fw.currentInfo.Height {
fw.currentInfo = info
fw.lastModTime = stat.ModTime()
fw.needsUpdate = true
return true
}
return false
}
func parseUpgradeInfoFile(filename string) (upgradetypes.Plan, error) {
var ui upgradetypes.Plan
f, err := os.Open(filename)
if err != nil {
return ui, err
}
defer f.Close()
d := json.NewDecoder(f)
err = d.Decode(&ui)
if err != nil {
return ui, err
}
// required values must be set
if ui.Height == 0 || ui.Name == "" {
return upgradetypes.Plan{}, fmt.Errorf("invalid upgrade-info.json content. Name and Hight must be not empty. Got: %v", ui)
}
return ui, err
}