diff --git a/center/center.go b/center/center.go index 89ec1cb1e5173d995b358ca5e8bdf51f810d0c77..ba143738cf78d7ece2406a9caa5b28e2e771955b 100644 --- a/center/center.go +++ b/center/center.go @@ -20,6 +20,7 @@ import ( "github.com/ccfos/nightingale/v6/pkg/httpx" "github.com/ccfos/nightingale/v6/pkg/i18nx" "github.com/ccfos/nightingale/v6/pkg/logx" + "github.com/ccfos/nightingale/v6/pkg/version" "github.com/ccfos/nightingale/v6/prom" "github.com/ccfos/nightingale/v6/pushgw/idents" "github.com/ccfos/nightingale/v6/pushgw/writer" @@ -85,6 +86,7 @@ func Initialize(configDir string, cryptoKey string) (func(), error) { writers := writer.NewWriters(config.Pushgw) httpx.InitRSAConfig(&config.HTTP.RSA) + go version.GetGithubVersion() alertrtRouter := alertrt.New(config.HTTP, config.Alert, alertMuteCache, targetCache, busiGroupCache, alertStats, ctx, externalProcessors) centerRouter := centerrt.New(config.HTTP, config.Center, cconf.Operations, dsCache, notifyConfigCache, promClients, redis, sso, ctx, metas, idents, targetCache, userCache, userGroupCache) diff --git a/center/router/router.go b/center/router/router.go index b0944236989b8423bd86dbfe6dbe22b5d8a26213..bb553cb3aec2cb1e4067ac53c92b043fafe01b76 100644 --- a/center/router/router.go +++ b/center/router/router.go @@ -26,7 +26,6 @@ import ( "github.com/rakyll/statik/fs" "github.com/toolkits/pkg/ginx" "github.com/toolkits/pkg/logger" - "github.com/toolkits/pkg/net/httplib" "github.com/toolkits/pkg/runner" ) @@ -388,14 +387,7 @@ func (rt *Router) Config(r *gin.Engine) { v = version.Version[:lastIndex] } - req := httplib.Get("https://api.github.com/repos/ccfos/nightingale/releases/latest") - var release GithubRelease - err := req.ToJSON(&release) - if err != nil { - ginx.NewRender(c).Data(gin.H{"version": v, "github_verison": ""}, nil) - } else { - ginx.NewRender(c).Data(gin.H{"version": v, "github_verison": release.TagName}, nil) - } + ginx.NewRender(c).Data(gin.H{"version": v, "github_verison": version.GithubVersion.Load().(string)}, nil) }) if rt.HTTP.APIForService.Enable { @@ -502,7 +494,3 @@ func Dangerous(c *gin.Context, v interface{}, code ...int) { c.JSON(http.StatusOK, gin.H{"error": t.Error()}) } } - -type GithubRelease struct { - TagName string `json:"tag_name"` -} diff --git a/pkg/version/version.go b/pkg/version/version.go index 534f798ceb5b899d75aea383c1dc3e92b7c5b4ee..7e401f858967603e99a5f8ac1a5527fc80109903 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,8 +1,16 @@ package version -import "github.com/hashicorp/go-version" +import ( + "sync/atomic" + "time" + + "github.com/hashicorp/go-version" + "github.com/toolkits/pkg/logger" + "github.com/toolkits/pkg/net/httplib" +) var Version = "unknown" +var GithubVersion atomic.Value func CompareVersion(v1, v2 string) (int, error) { version1, err := version.NewVersion(v1) @@ -22,3 +30,21 @@ func CompareVersion(v1, v2 string) (int, error) { } return 0, nil } + +func GetGithubVersion() { + for { + req := httplib.Get("https://api.github.com/repos/ccfos/nightingale/releases/latest") + var release GithubRelease + err := req.ToJSON(&release) + if err != nil { + logger.Errorf("get github version fail: %v", err) + } + + GithubVersion.Store(release.TagName) + time.Sleep(24 * time.Hour) + } +} + +type GithubRelease struct { + TagName string `json:"tag_name"` +}