Skip to content

Commit

Permalink
Merge pull request #16 from hamster1963/refactor-cache-key
Browse files Browse the repository at this point in the history
重构了变量管理
  • Loading branch information
hamster1963 authored Jul 17, 2023
2 parents 1dce63e + a1069da commit 5f6b3f1
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 37 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/GoCompileTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ jobs:
echo "${{env.GIT_COMMIT_LOG}}"
echo "${{env.BUILD_TIME}}"

- name: Install GF CLI dependencies
- name: Go Build Binary - Windows amd64
run: |
wget -O gf "https://github.com/gogf/gf/releases/latest/download/gf_$(go env GOOS)_$(go env GOARCH)" && chmod +x gf && ./gf install -y && rm ./gf

- name: GF CLI Build Binary
run: |
gf build
GOOS=windows GOARCH=amd64 go build
pwd

13 changes: 7 additions & 6 deletions internal/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/glog"
"kes-cron/internal/global/g_consts"
"kes-cron/internal/global/g_cache"
"kes-cron/internal/global/g_structs"
"kes-cron/utility/cli_utils"
"kes-cron/utility/cron_utils"
"kes-cron/utility/net_utils"
"kes-cron/utility/update_utils"
)

func Boot(initData *g_consts.InitData) (err error) {
func Boot(initData *g_structs.InitData) (err error) {
_, err = gcron.AddOnce(context.TODO(), "@every 1s", func(ctx context.Context) {
glog.Debug(context.Background(), "定时任务启动中...")
if err := bootMethod(initData); err != nil {
Expand All @@ -39,7 +40,7 @@ func Boot(initData *g_consts.InitData) (err error) {
}

// bootCheck 测试初次启动任务
func bootCheck(initData *g_consts.InitData) (err error) {
func bootCheck(initData *g_structs.InitData) (err error) {
err = cli_utils.CmdCore.StartSpeedCmd(context.Background(), initData)
if err != nil {
glog.Error(context.Background(), "测试测速服务", err)
Expand All @@ -55,7 +56,7 @@ func bootCheck(initData *g_consts.InitData) (err error) {
}

// bootMethod 初始化定时任务
func bootMethod(initData *g_consts.InitData) (err error) {
func bootMethod(initData *g_structs.InitData) (err error) {
var ctx = context.TODO()

glog.Debug(ctx, "开始初始化定时任务管理器")
Expand All @@ -79,8 +80,8 @@ func bootMethod(initData *g_consts.InitData) (err error) {
glog.Error(ctx, "自动更新服务失败: ", err)
return
}
if !gcache.MustGet(ctx, "updateStatus").IsNil() {
_, _ = gcache.Remove(ctx, "updateStatus")
if !gcache.MustGet(ctx, g_cache.UpdateCacheKey).IsNil() {
_, _ = gcache.Remove(ctx, g_cache.UpdateCacheKey)
}
}, "Cron-Update")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/os/gres"
"kes-cron/internal/boot"
"kes-cron/internal/global/g_consts"
"kes-cron/internal/global/g_structs"
"kes-cron/utility/cron_utils"
)

Expand Down Expand Up @@ -39,7 +39,7 @@ var (
}

// 初始化数据
initData := &g_consts.InitData{
initData := &g_structs.InitData{
Department: parser.GetOpt("department").String(),
Name: parser.GetOpt("name").String(),
}
Expand Down
6 changes: 6 additions & 0 deletions internal/global/g_cache/cache_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package g_cache

const (
UpdateCacheKey = "updateStatus"
SpeedCacheKey = "speedStatus"
)
11 changes: 6 additions & 5 deletions internal/global/g_consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package g_consts

const (
PingUrl = "https://www.ithome.com/"
SpeedBackendUrl = "http://120.24.211.49:10441/UploadSpeedData"
PingBackendUrl = "http://120.24.211.49:10441/UploadPingData"
AuthBackendUrl = "http://120.24.211.49:10441/DeviceAuth"
ConfigBackendUrl = "http://120.24.211.49:10441/GetCronConfig"
UpdateBackendUrl = "http://120.24.211.49:10441/GetLatestVersion"
BackendBaseUrl = "http://120.24.211.49:10441"
SpeedBackendUrl = BackendBaseUrl + "/UploadSpeedData"
PingBackendUrl = BackendBaseUrl + "/UploadPingData"
AuthBackendUrl = BackendBaseUrl + "/DeviceAuth"
ConfigBackendUrl = BackendBaseUrl + "/GetCronConfig"
UpdateBackendUrl = BackendBaseUrl + "/GetLatestVersion"
DownloadExeUrl = "https://gh.xinyu.today/https://github.com/hamster1963/Speed-Cron/releases/latest/download/speed_cron_windows_amd64.exe"
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package g_consts
package g_structs

type InitData struct {
Department string `json:"department" description:"部门"`
Expand Down
7 changes: 4 additions & 3 deletions utility/cli_utils/cmd_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/glog"
"kes-cron/internal/global/g_consts"
"kes-cron/internal/global/g_cache"
"kes-cron/internal/global/g_structs"
)

type uCmdCore struct{}
Expand All @@ -17,7 +18,7 @@ var CmdCore = &uCmdCore{}
//
// @dc: 启动speedtest命令
// @author: hamster @date:2023/6/20 10:06:06
func (u *uCmdCore) StartSpeedCmd(ctx context.Context, initData *g_consts.InitData) (err error) {
func (u *uCmdCore) StartSpeedCmd(ctx context.Context, initData *g_structs.InitData) (err error) {
cmd := CliUtils.CreateSpeedCmd()
if cmd == nil {
glog.Warning(ctx, "创建命令失败,获取测速节点失败")
Expand Down Expand Up @@ -48,7 +49,7 @@ func (u *uCmdCore) StartSpeedCmd(ctx context.Context, initData *g_consts.InitDat
// 持续获取输出
for scanner.Scan() {
// 获取更新核心状态,判断是否即将更新
if gcache.MustGet(ctx, "speedtestStatus").Bool() {
if gcache.MustGet(ctx, g_cache.UpdateCacheKey).Bool() {
glog.Warning(ctx, "正在更新客户端程序,中止测速服务")
break
}
Expand Down
7 changes: 4 additions & 3 deletions utility/cron_utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/os/glog"
"kes-cron/internal/global/g_consts"
"kes-cron/internal/global/g_structs"
"kes-cron/utility/net_utils"
"time"
)
Expand All @@ -20,7 +21,7 @@ var Auth = &uAuth{}
//
// @dc: 设备认证
// @author: hamster @date:2023/6/20 11:42:38
func (u *uAuth) DeviceAuth(initData *g_consts.InitData) (err error) {
func (u *uAuth) DeviceAuth(initData *g_structs.InitData) (err error) {
// 获取内网与mac地址
internalIp, macAddress := net_utils.NetworkInfo.GetMacAddress()
// 进行设备认证
Expand Down Expand Up @@ -51,7 +52,7 @@ func (u *uAuth) DeviceAuth(initData *g_consts.InitData) (err error) {
//
// @dc: 获取设备信息
// @author: hamster @date:2023/6/20 17:34:24
func (u *uAuth) GetDeviceInfo() (getInitData *g_consts.InitData, err error) {
func (u *uAuth) GetDeviceInfo() (getInitData *g_structs.InitData, err error) {
// 获取Mac地址
_, macAddress := net_utils.NetworkInfo.GetMacAddress()
// 获取配置
Expand All @@ -74,7 +75,7 @@ func (u *uAuth) GetDeviceInfo() (getInitData *g_consts.InitData, err error) {
}
// 解析配置
configMap := gjson.New(response.ReadAllString())
getInitData = &g_consts.InitData{
getInitData = &g_structs.InitData{
Department: configMap.Get("data.department").String(),
Name: configMap.Get("data.staff_name").String(),
}
Expand Down
14 changes: 8 additions & 6 deletions utility/cron_utils/cron_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/glog"
"kes-cron/internal/global/g_cache"
"kes-cron/internal/global/g_consts"
"kes-cron/internal/global/g_structs"
"kes-cron/utility/cli_utils"
"kes-cron/utility/net_utils"
"reflect"
Expand All @@ -24,7 +26,7 @@ var CronManage = &uCronManage{}
//
// @dc: 获取定时任务管理器配置并启动
// @author: hamster @date:2023/6/20 13:41:26
func (u *uCronManage) GetConfigAndStart(ctx context.Context, initData *g_consts.InitData) (err error) {
func (u *uCronManage) GetConfigAndStart(ctx context.Context, initData *g_structs.InitData) (err error) {
glog.Debug(ctx, "开始获取定时任务管理器配置")
// 设备认证
err = Auth.DeviceAuth(initData)
Expand Down Expand Up @@ -157,23 +159,23 @@ func getConfig() (speedInterval string, pingInterval string, cronStatus int, err
}

// addSpeedCron 添加测速定时任务
func addSpeedCron(ctx context.Context, initData *g_consts.InitData, timePattern string) (err error) {
func addSpeedCron(ctx context.Context, initData *g_structs.InitData, timePattern string) (err error) {
glog.Notice(ctx, "开始定时测速服务", timePattern)
_, err = gcron.AddSingleton(ctx, timePattern, func(ctx context.Context) {
// 判断是否在更新中
if gcache.MustGet(ctx, "speedtestStatus").Bool() {
if gcache.MustGet(ctx, g_cache.UpdateCacheKey).Bool() {
glog.Warning(ctx, "正在更新客户端程序,跳过本次测速")
return
}
_ = gcache.Set(ctx, "speedtestStatus", true, 1*time.Minute)
_ = gcache.Set(ctx, g_cache.SpeedCacheKey, true, 1*time.Minute)

err := cli_utils.CmdCore.StartSpeedCmd(ctx, initData)
if err != nil {
glog.Error(ctx, "定时测速服务失败: ", err)
return
}

_, _ = gcache.Remove(ctx, "speedtestStatus")
_, _ = gcache.Remove(ctx, g_cache.SpeedCacheKey)
}, "Speed-Cron")
if err != nil {
glog.Warning(ctx, "添加定时测速服务失败: ", err)
Expand All @@ -183,7 +185,7 @@ func addSpeedCron(ctx context.Context, initData *g_consts.InitData, timePattern
}

// addPingCron 添加延迟检测定时任务
func addPingCron(ctx context.Context, initData *g_consts.InitData, timePattern string) (err error) {
func addPingCron(ctx context.Context, initData *g_structs.InitData, timePattern string) (err error) {
glog.Notice(ctx, "开始HTTPS延迟定时检测服务", timePattern)
_, err = gcron.AddSingleton(ctx, timePattern, func(ctx context.Context) {
err := net_utils.NetUtils.CoreLatency(initData)
Expand Down
5 changes: 3 additions & 2 deletions utility/net_utils/https_latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/gogf/gf/v2/util/gconv"
"io"
"kes-cron/internal/global/g_consts"
"kes-cron/internal/global/g_structs"
"net/http"
"time"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func (u *uNetUtils) HttpsLatency() (latency int, err error) {
//
// @dc: 测试延迟核心服务
// @author: Hamster @date:2023-06-17 14:03:41
func (u *uNetUtils) CoreLatency(initData *g_consts.InitData) (err error) {
func (u *uNetUtils) CoreLatency(initData *g_structs.InitData) (err error) {
latency, err := u.HttpsLatency()
if err != nil {
glog.Warning(context.Background(), "请求出错:", err)
Expand All @@ -59,7 +60,7 @@ func (u *uNetUtils) CoreLatency(initData *g_consts.InitData) (err error) {
//
// @dc: 推送延迟到服务器
// @author: Hamster @date:2023-06-17 14:02:59
func (u *uNetUtils) PushLatencyToServer(initData *g_consts.InitData, latency int) (err error) {
func (u *uNetUtils) PushLatencyToServer(initData *g_structs.InitData, latency int) (err error) {
_, macAddress := NetworkInfo.GetMacAddress()
params := g.Map{
"department": initData.Department,
Expand Down
8 changes: 5 additions & 3 deletions utility/update_utils/auto_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/gogf/gf/v2/os/glog"
"github.com/kardianos/osext"
"gopkg.in/inconshreveable/go-update.v0"
"kes-cron/internal/global/g_cache"
"kes-cron/internal/global/g_consts"
"kes-cron/internal/global/g_structs"
"os"
"path/filepath"
"time"
Expand All @@ -25,9 +27,9 @@ var AutoUpdate = &uAutoUpdate{}
//
// @dc: 更新任务核心程序
// @author: laixin @date:2023/7/14 09:37:24
func (u *uAutoUpdate) UpdateCore(ctx context.Context, initData *g_consts.InitData) (err error) {
func (u *uAutoUpdate) UpdateCore(ctx context.Context, initData *g_structs.InitData) (err error) {
// 检测是否在进行测速服务
if gcache.MustGet(ctx, "speedtestStatus").Bool() {
if gcache.MustGet(ctx, g_cache.SpeedCacheKey).Bool() {
glog.Warning(ctx, "正在进行测速服务,无法更新")
return nil
}
Expand All @@ -46,7 +48,7 @@ func (u *uAutoUpdate) UpdateCore(ctx context.Context, initData *g_consts.InitDat
}

// 设置更新状态缓存
_ = gcache.Set(ctx, "updateStatus", true, 0)
_ = gcache.Set(ctx, g_cache.UpdateCacheKey, true, 0)

glog.Debug(ctx, "开始更新speed_cron...")
err = updateFunc()
Expand Down

0 comments on commit 5f6b3f1

Please sign in to comment.