feat: 添加定时系统

- 实现定时任务
This commit is contained in:
2026-03-26 22:59:58 +08:00
parent 858c29a799
commit b0db64bd7b
8 changed files with 126 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
package ckwk
import (
"ckwk/internal/conf"
"ckwk/pkg/common"
"ckwk/pkg/log"
"ckwk/pkg/request"
@@ -97,7 +98,7 @@ func (wk *WK) Code() (string, error) {
"png_fix": "false",
}).
SetResult(&result).
Post("http://localhost:8000/ocr")
Post(fmt.Sprintf("%s/ocr", conf.DdddOCR))
if err != nil {
return "", fmt.Errorf("获取验证码验证结果失败: %w", err)
}

View File

@@ -115,3 +115,44 @@ func (m *SessionManager) KeepAlive(ctx context.Context, id string, wk *WK) {
}
}
}
func (m *SessionManager) ClearAll() {
m.mu.Lock()
defer m.mu.Unlock()
for sessionID, item := range m.sessions {
// 停止 KeepAlive
if item.cancel != nil {
item.cancel()
}
userKey := item.Instance.Host + ":" + item.Instance.Username
delete(m.userToSession, userKey)
delete(m.sessions, sessionID)
log.Info("清理 Session", zap.String("id", sessionID))
}
log.Info("所有 Session 已清空")
}
func (m *SessionManager) ClearExpired(d time.Duration) {
m.mu.Lock()
defer m.mu.Unlock()
now := time.Now()
for sessionID, item := range m.sessions {
if now.Sub(item.LastValue) > d {
if item.cancel != nil {
item.cancel()
}
userKey := item.Instance.Host + ":" + item.Instance.Username
delete(m.userToSession, userKey)
delete(m.sessions, sessionID)
log.Info("清理过期 Session", zap.String("id", sessionID))
}
}
}