From 577802e5ada5a934bb63ca86f251d0c88a415c84 Mon Sep 17 00:00:00 2001 From: "meilin.huang" <954537473@qq.com> Date: Sun, 24 Jul 2022 15:37:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/go.mod | 4 +- server/internal/constant/constant.go | 8 +-- .../infrastructure/machine/sshtunnel.go | 52 ++++++++++--------- .../devops/infrastructure/scheudler/mytask.go | 27 ---------- .../scheudler => pkg/scheduler}/scheduler.go | 4 ++ 5 files changed, 37 insertions(+), 58 deletions(-) delete mode 100644 server/internal/devops/infrastructure/scheudler/mytask.go rename server/{internal/devops/infrastructure/scheudler => pkg/scheduler}/scheduler.go (94%) diff --git a/server/go.mod b/server/go.mod index 2e05a017..a90f82d2 100644 --- a/server/go.mod +++ b/server/go.mod @@ -12,7 +12,7 @@ require ( github.com/mojocn/base64Captcha v1.3.5 // 验证码 github.com/pkg/sftp v1.13.5 github.com/robfig/cron/v3 v3.0.1 // 定时任务 - github.com/sirupsen/logrus v1.8.1 + github.com/sirupsen/logrus v1.9.0 github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 go.mongodb.org/mongo-driver v1.9.1 // mongo golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // ssh @@ -52,7 +52,7 @@ require ( golang.org/x/image v0.0.0-20220302094943-723b81ca9867 // indirect golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect - golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/protobuf v1.28.0 // indirect diff --git a/server/internal/constant/constant.go b/server/internal/constant/constant.go index bd81f009..b0925b1e 100644 --- a/server/internal/constant/constant.go +++ b/server/internal/constant/constant.go @@ -9,8 +9,8 @@ const ( MongoConnExpireTime = 30 * time.Minute /**** 开发测试使用 ****/ -// MachineConnExpireTime = 20 * time.Second -// DbConnExpireTime = 20 * time.Second -// RedisConnExpireTime = 20 * time.Second -// MongoConnExpireTime = 20 * time.Second +// MachineConnExpireTime = 4 * time.Minute +// DbConnExpireTime = 2 * time.Minute +// RedisConnExpireTime = 2 * time.Minute +// MongoConnExpireTime = 2 * time.Minute ) diff --git a/server/internal/devops/infrastructure/machine/sshtunnel.go b/server/internal/devops/infrastructure/machine/sshtunnel.go index 9f176c85..4f8f56b7 100644 --- a/server/internal/devops/infrastructure/machine/sshtunnel.go +++ b/server/internal/devops/infrastructure/machine/sshtunnel.go @@ -5,11 +5,11 @@ import ( "io" "mayfly-go/internal/devops/domain/entity" "mayfly-go/pkg/global" + "mayfly-go/pkg/scheduler" "mayfly-go/pkg/utils" "net" "os" "sync" - "time" "golang.org/x/crypto/ssh" ) @@ -31,30 +31,29 @@ type CheckSshTunnelMachineHasUseFunc func(uint64) bool func startCheckUse() { global.Log.Info("开启定时检测ssh隧道机器是否还有被使用") - heartbeat := time.Duration(10) * time.Minute - tick := time.NewTicker(heartbeat) - go func() { - for range tick.C { - func() { - if !mutex.TryLock() { - return - } - defer mutex.Unlock() - // 遍历隧道机器,都未被使用将会被关闭 - for mid, sshTunnelMachine := range sshTunnelMachines { - global.Log.Debugf("开始定时检查ssh隧道机器[%d]是否还有被使用...", mid) - for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs { - // 如果一个在使用则返回不关闭,不继续后续检查 - if checkUseFunc(mid) { - return - } - } - // 都未被使用,则关闭 - sshTunnelMachine.Close() - } - }() + // 每十分钟检查一次隧道机器是否还有被使用 + scheduler.AddFun("@every 10m", func() { + if !mutex.TryLock() { + return } - }() + defer mutex.Unlock() + // 遍历隧道机器,都未被使用将会被关闭 + for mid, sshTunnelMachine := range sshTunnelMachines { + global.Log.Debugf("开始定时检查ssh隧道机器[%d]是否还有被使用...", mid) + hasUse := false + for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs { + // 如果一个在使用则返回不关闭,不继续后续检查 + if checkUseFunc(mid) { + hasUse = true + break + } + } + if !hasUse { + // 都未被使用,则关闭 + sshTunnelMachine.Close() + } + } + }) } // 添加ssh隧道机器检测是否使用函数 @@ -129,7 +128,10 @@ func (stm *SshTunnelMachine) Close() { if stm.SshClient != nil { global.Log.Infof("ssh隧道机器[%d]未被使用, 关闭隧道...", stm.machineId) - stm.SshClient.Close() + err := stm.SshClient.Close() + if err != nil { + global.Log.Errorf("关闭ssh隧道机器[%s]发生错误: %s", stm.machineId, err.Error()) + } } delete(sshTunnelMachines, stm.machineId) } diff --git a/server/internal/devops/infrastructure/scheudler/mytask.go b/server/internal/devops/infrastructure/scheudler/mytask.go deleted file mode 100644 index 53fb0f30..00000000 --- a/server/internal/devops/infrastructure/scheudler/mytask.go +++ /dev/null @@ -1,27 +0,0 @@ -package scheduler - -func init() { - SaveMachineMonitor() -} - -func SaveMachineMonitor() { - AddFun("@every 60s", func() { - // for _, m := range models.GetNeedMonitorMachine() { - // m := m - // go func() { - // cli, err := machine.GetCli(uint64(utils.GetInt4Map(m, "id"))) - // if err != nil { - // mlog.Log.Error("获取客户端失败:", err.Error()) - // return - // } - // mm := cli.GetMonitorInfo() - // if mm != nil { - // err := model.Insert(mm) - // if err != nil { - // mlog.Log.Error("保存机器监控信息失败: ", err.Error()) - // } - // } - // }() - // } - }) -} diff --git a/server/internal/devops/infrastructure/scheudler/scheduler.go b/server/pkg/scheduler/scheduler.go similarity index 94% rename from server/internal/devops/infrastructure/scheudler/scheduler.go rename to server/pkg/scheduler/scheduler.go index 31fef6a0..a848a7e2 100644 --- a/server/internal/devops/infrastructure/scheudler/scheduler.go +++ b/server/pkg/scheduler/scheduler.go @@ -6,6 +6,10 @@ import ( "github.com/robfig/cron/v3" ) +func init() { + Start() +} + var cronService = cron.New() func Start() {