版本更新改为协程,预防拥堵主程序

This commit is contained in:
2025-10-31 14:29:07 +08:00
parent 03f762e910
commit 8b943b0cca
5 changed files with 27 additions and 35 deletions

View File

@@ -2,29 +2,6 @@ package system
import "time"
type T struct {
Url string `json:"url"`
AssetsUrl string `json:"assets_url"`
UploadUrl string `json:"upload_url"`
HtmlUrl string `json:"html_url"`
Id int `json:"id"`
Author *Author `json:"author"`
NodeId string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Immutable bool `json:"immutable"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PublishedAt time.Time `json:"published_at"`
Assets []*Assets
TarballUrl string `json:"tarball_url"`
ZipballUrl string `json:"zipball_url"`
Body string `json:"body"`
}
type Author struct {
Login string `json:"login"`
Id int `json:"id"`

View File

@@ -4,6 +4,7 @@ import (
"context"
"github.com/ayflying/p2p/internal/service"
updateGithub "github.com/ayflying/update-github-release"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/gctx"
@@ -20,18 +21,23 @@ func init() {
getDev, _ := g.Cfg().GetWithEnv(gctx.New(), "dev")
if !getDev.Bool() {
var update = updateGithub.New("https://api.github.com/repos/ayflying/p2p/releases/latest")
// 每天0点检查更新
gcron.Add(gctx.New(), "0 0 0 * * *", func(ctx context.Context) {
err := service.System().CheckUpdate()
err := update.CheckUpdate()
if err != nil {
g.Log().Errorf(ctx, "检查更新失败:%v", err)
}
})
err := service.System().CheckUpdate()
if err != nil {
g.Log().Errorf(gctx.New(), "检查更新失败:%v", err)
}
go func() {
//在协程中检查更新,预防主程序阻塞
err := update.CheckUpdate()
if err != nil {
g.Log().Errorf(gctx.New(), "检查更新失败:%v", err)
}
}()
} else {
g.Log().Debugf(gctx.New(), "开发模式,不检查更新")
}