修复关闭的会话仍然显示在线的问题 fixes #25

This commit is contained in:
dushixiang
2021-01-12 21:37:27 +08:00
parent 6f2bf6c9ba
commit 3bb7d2d49b
7 changed files with 49 additions and 23 deletions

View File

@ -1,32 +1,53 @@
package handle
import (
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"next-terminal/pkg/api"
"next-terminal/pkg/global"
"next-terminal/pkg/guacd"
"next-terminal/pkg/model"
"next-terminal/pkg/utils"
"os"
"strconv"
"time"
)
func RunTicker() {
var ch chan int
c := cron.New(cron.WithSeconds()) //精确到秒
// 定时任务,每隔一小时删除一次未使用的会话信息
ticker := time.NewTicker(time.Minute * 60)
go func() {
for range ticker.C {
sessions, _ := model.FindSessionByStatus(model.NoConnect)
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)
}
_, _ = c.AddFunc("0 0/1 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.Debugf("会话「%v」ID「%v」超过1小时未打开已删除。", s, sessions[i].ID)
}
}
}
ch <- 1
}()
<-ch
})
// 定时任务,每隔一分钟校验一次运行中的会话信息
_, _ = c.AddFunc("0 0/1 0/1 * * ?", func() {
sessions, _ := model.FindSessionByStatus(model.Connected)
if sessions != nil && len(sessions) > 0 {
for i := range sessions {
_, found := global.Cache.Get(sessions[i].ID)
if !found {
api.CloseSessionById(sessions[i].ID, api.Normal, "")
s := sessions[i].Username + "@" + sessions[i].IP + ":" + strconv.Itoa(sessions[i].Port)
logrus.Debugf("会话「%v」ID「%v」已离线修改状态为「关闭」。", s, sessions[i].ID)
}
}
}
})
c.Start()
}
func RunDataFix() {