-
Notifications
You must be signed in to change notification settings - Fork 288
/
version_checker.go
61 lines (51 loc) · 1.44 KB
/
version_checker.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
package engine
import (
"context"
"strings"
"time"
"github.com/windmilleng/tilt/internal/github"
"github.com/windmilleng/tilt/internal/logger"
"github.com/windmilleng/tilt/internal/store"
)
const githubOrg = "windmilleng"
const githubProject = "tilt"
const versionCheckInterval = time.Hour * 4
type TiltVersionChecker struct {
started bool
clientFactory GithubClientFactory
client github.Client
timerMaker timerMaker
}
func NewTiltVersionChecker(ghcf GithubClientFactory, timerMaker timerMaker) *TiltVersionChecker {
return &TiltVersionChecker{clientFactory: ghcf, timerMaker: timerMaker}
}
type GithubClientFactory func() github.Client
func NewGithubClientFactory() GithubClientFactory {
return github.NewClient
}
func (tvc *TiltVersionChecker) OnChange(ctx context.Context, st store.RStore) {
if tvc.started {
return
}
tvc.client = tvc.clientFactory()
tvc.started = true
go func() {
for {
version, err := tvc.client.GetLatestRelease(ctx, githubOrg, githubProject)
if err != nil {
if strings.Contains(err.Error(), "API rate limit exceeded") {
logger.Get(ctx).Verbosef("error checking github for latest Tilt release: %v", err)
} else {
logger.Get(ctx).Infof("error checking github for latest Tilt release: %s", err.Error())
}
} else {
st.Dispatch(LatestVersionAction{version})
}
select {
case <-tvc.timerMaker(versionCheckInterval):
case <-ctx.Done():
return
}
}
}()
}