增加可配置的定时任务功能
This commit is contained in:
@ -1,81 +1,23 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
"log"
|
||||
"next-terminal/pkg/global"
|
||||
"next-terminal/pkg/guacd"
|
||||
"next-terminal/pkg/model"
|
||||
"next-terminal/pkg/utils"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RunTicker() {
|
||||
|
||||
c := cron.New(cron.WithSeconds()) //精确到秒
|
||||
// 每隔一小时删除一次未使用的会话信息
|
||||
_, _ = global.Cron.AddJob("0 0 0/1 * * ?", model.DelUnUsedSessionJob{})
|
||||
// 每隔一小时检测一次资产状态
|
||||
//_, _ = global.Cron.AddJob("0 0 0/1 * * ?", model.CheckAssetStatusJob{})
|
||||
// 每日凌晨删除超过时长限制的会话
|
||||
//_, _ = global.Cron.AddJob("0 0 0 * * ?", model.DelTimeoutSessionJob{})
|
||||
|
||||
_, _ = c.AddFunc("0 0 0/1 * * ?", func() {
|
||||
// 定时任务,每隔一小时删除一次未使用的会话信息
|
||||
sessions, _ := model.FindSessionByStatusIn([]string{model.NoConnect, model.Connecting})
|
||||
if sessions != nil && len(sessions) > 0 {
|
||||
now := time.Now()
|
||||
for i := range sessions {
|
||||
if now.Sub(sessions[i].ConnectedTime.Time) > time.Hour*1 {
|
||||
_ = model.DeleteSessionById(sessions[i].ID)
|
||||
s := sessions[i].Username + "@" + sessions[i].IP + ":" + strconv.Itoa(sessions[i].Port)
|
||||
logrus.Infof("会话「%v」ID「%v」超过1小时未打开,已删除。", s, sessions[i].ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 每隔一小时检测一次资产是否存活
|
||||
assets, _ := model.FindAllAsset()
|
||||
if assets != nil && len(assets) > 0 {
|
||||
for i := range assets {
|
||||
asset := assets[i]
|
||||
active := utils.Tcping(asset.IP, asset.Port)
|
||||
model.UpdateAssetActiveById(active, asset.ID)
|
||||
logrus.Infof("资产「%v」ID「%v」存活状态检测完成,存活「%v」。", asset.Name, asset.ID, active)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
_, err := c.AddFunc("0 0 0 * * ?", func() {
|
||||
// 定时任务 每日凌晨检查超过时长限制的会话
|
||||
property, err := model.FindPropertyByName("session-saved-limit")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if property.Value == "" || property.Value == "-" {
|
||||
return
|
||||
}
|
||||
limit, err := strconv.Atoi(property.Value)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sessions, err := model.FindOutTimeSessions(limit)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if sessions != nil && len(sessions) > 0 {
|
||||
var sessionIds []string
|
||||
for i := range sessions {
|
||||
sessionIds = append(sessionIds, sessions[i].ID)
|
||||
}
|
||||
err := model.DeleteSessionByIds(sessionIds)
|
||||
if err != nil {
|
||||
logrus.Errorf("删除离线会话失败 %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
c.Start()
|
||||
global.Cron.Start()
|
||||
}
|
||||
|
||||
func RunDataFix() {
|
||||
|
Reference in New Issue
Block a user