-
Notifications
You must be signed in to change notification settings - Fork 199
/
softwareVersion.go
99 lines (84 loc) · 2.81 KB
/
softwareVersion.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
package softwareVersion
import (
"context"
"time"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/common"
)
type tagVersion struct {
TagVersion string `json:"tag_name"`
}
// SoftwareVersionChecker is a component which is used to check if a new software stable tag is available
type SoftwareVersionChecker struct {
statusHandler core.AppStatusHandler
stableTagProvider StableTagProviderHandler
mostRecentSoftwareVersion string
checkInterval time.Duration
closeFunc func()
}
var log = logger.GetOrCreate("common/statistics")
// NewSoftwareVersionChecker will create an object for software version checker
func NewSoftwareVersionChecker(
appStatusHandler core.AppStatusHandler,
stableTagProvider StableTagProviderHandler,
pollingIntervalInMinutes int,
) (*SoftwareVersionChecker, error) {
if check.IfNil(appStatusHandler) {
return nil, core.ErrNilAppStatusHandler
}
if check.IfNil(stableTagProvider) {
return nil, core.ErrNilStatusTagProvider
}
if pollingIntervalInMinutes <= 0 {
return nil, core.ErrInvalidPollingInterval
}
checkInterval := time.Duration(pollingIntervalInMinutes) * time.Minute
return &SoftwareVersionChecker{
statusHandler: appStatusHandler,
stableTagProvider: stableTagProvider,
mostRecentSoftwareVersion: "",
checkInterval: checkInterval,
closeFunc: nil,
}, nil
}
// StartCheckSoftwareVersion will check on a specific interval if a new software version is available
func (svc *SoftwareVersionChecker) StartCheckSoftwareVersion() {
ctx, cancelFunc := context.WithCancel(context.Background())
svc.closeFunc = cancelFunc
go svc.checkSoftwareVersion(ctx)
}
func (svc *SoftwareVersionChecker) checkSoftwareVersion(ctx context.Context) {
for {
svc.readLatestStableVersion()
select {
case <-ctx.Done():
log.Debug("softwareVersionChecker's go routine is stopping...")
return
case <-time.After(svc.checkInterval):
}
}
}
func (svc *SoftwareVersionChecker) readLatestStableVersion() {
tagVersionFromURL, err := svc.stableTagProvider.FetchTagVersion()
if err != nil {
log.Debug("cannot read json with latest stable tag", "error", err)
return
}
if tagVersionFromURL != "" {
svc.mostRecentSoftwareVersion = tagVersionFromURL
}
svc.statusHandler.SetStringValue(common.MetricLatestTagSoftwareVersion, svc.mostRecentSoftwareVersion)
}
// IsInterfaceNil returns true if there is no value under the interface
func (svc *SoftwareVersionChecker) IsInterfaceNil() bool {
return svc == nil
}
// Close will handle the closing of opened go routines
func (svc *SoftwareVersionChecker) Close() error {
if svc.closeFunc != nil {
svc.closeFunc()
}
return nil
}