mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-23 17:40:25 +08:00
fix: 定时任务问题修复
This commit is contained in:
@@ -12,7 +12,7 @@ require (
|
|||||||
github.com/mojocn/base64Captcha v1.3.5 // 验证码
|
github.com/mojocn/base64Captcha v1.3.5 // 验证码
|
||||||
github.com/pkg/sftp v1.13.5
|
github.com/pkg/sftp v1.13.5
|
||||||
github.com/robfig/cron/v3 v3.0.1 // 定时任务
|
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
|
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
|
||||||
go.mongodb.org/mongo-driver v1.9.1 // mongo
|
go.mongodb.org/mongo-driver v1.9.1 // mongo
|
||||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // ssh
|
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/image v0.0.0-20220302094943-723b81ca9867 // indirect
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // 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/text v0.3.7 // indirect
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/protobuf v1.28.0 // indirect
|
google.golang.org/protobuf v1.28.0 // indirect
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ const (
|
|||||||
MongoConnExpireTime = 30 * time.Minute
|
MongoConnExpireTime = 30 * time.Minute
|
||||||
|
|
||||||
/**** 开发测试使用 ****/
|
/**** 开发测试使用 ****/
|
||||||
// MachineConnExpireTime = 20 * time.Second
|
// MachineConnExpireTime = 4 * time.Minute
|
||||||
// DbConnExpireTime = 20 * time.Second
|
// DbConnExpireTime = 2 * time.Minute
|
||||||
// RedisConnExpireTime = 20 * time.Second
|
// RedisConnExpireTime = 2 * time.Minute
|
||||||
// MongoConnExpireTime = 20 * time.Second
|
// MongoConnExpireTime = 2 * time.Minute
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"mayfly-go/internal/devops/domain/entity"
|
"mayfly-go/internal/devops/domain/entity"
|
||||||
"mayfly-go/pkg/global"
|
"mayfly-go/pkg/global"
|
||||||
|
"mayfly-go/pkg/scheduler"
|
||||||
"mayfly-go/pkg/utils"
|
"mayfly-go/pkg/utils"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
@@ -31,11 +31,8 @@ type CheckSshTunnelMachineHasUseFunc func(uint64) bool
|
|||||||
|
|
||||||
func startCheckUse() {
|
func startCheckUse() {
|
||||||
global.Log.Info("开启定时检测ssh隧道机器是否还有被使用")
|
global.Log.Info("开启定时检测ssh隧道机器是否还有被使用")
|
||||||
heartbeat := time.Duration(10) * time.Minute
|
// 每十分钟检查一次隧道机器是否还有被使用
|
||||||
tick := time.NewTicker(heartbeat)
|
scheduler.AddFun("@every 10m", func() {
|
||||||
go func() {
|
|
||||||
for range tick.C {
|
|
||||||
func() {
|
|
||||||
if !mutex.TryLock() {
|
if !mutex.TryLock() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -43,18 +40,20 @@ func startCheckUse() {
|
|||||||
// 遍历隧道机器,都未被使用将会被关闭
|
// 遍历隧道机器,都未被使用将会被关闭
|
||||||
for mid, sshTunnelMachine := range sshTunnelMachines {
|
for mid, sshTunnelMachine := range sshTunnelMachines {
|
||||||
global.Log.Debugf("开始定时检查ssh隧道机器[%d]是否还有被使用...", mid)
|
global.Log.Debugf("开始定时检查ssh隧道机器[%d]是否还有被使用...", mid)
|
||||||
|
hasUse := false
|
||||||
for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs {
|
for _, checkUseFunc := range checkSshTunnelMachineHasUseFuncs {
|
||||||
// 如果一个在使用则返回不关闭,不继续后续检查
|
// 如果一个在使用则返回不关闭,不继续后续检查
|
||||||
if checkUseFunc(mid) {
|
if checkUseFunc(mid) {
|
||||||
return
|
hasUse = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !hasUse {
|
||||||
// 都未被使用,则关闭
|
// 都未被使用,则关闭
|
||||||
sshTunnelMachine.Close()
|
sshTunnelMachine.Close()
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加ssh隧道机器检测是否使用函数
|
// 添加ssh隧道机器检测是否使用函数
|
||||||
@@ -129,7 +128,10 @@ func (stm *SshTunnelMachine) Close() {
|
|||||||
|
|
||||||
if stm.SshClient != nil {
|
if stm.SshClient != nil {
|
||||||
global.Log.Infof("ssh隧道机器[%d]未被使用, 关闭隧道...", stm.machineId)
|
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)
|
delete(sshTunnelMachines, stm.machineId)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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())
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }()
|
|
||||||
// }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,10 @@ import (
|
|||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Start()
|
||||||
|
}
|
||||||
|
|
||||||
var cronService = cron.New()
|
var cronService = cron.New()
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
Reference in New Issue
Block a user