feat: add a service for periodic cleanup of error accounts based on configuration.
This commit is contained in:
@@ -104,8 +104,9 @@ func startServer(cfg *config.Config) {
|
||||
// 日志 API
|
||||
mux.HandleFunc("/api/logs", api.CORS(handleGetLogs))
|
||||
mux.HandleFunc("/api/logs/clear", api.CORS(handleClearLogs))
|
||||
mux.HandleFunc("/api/logs/query", api.CORS(handleQueryLogs)) // 按模块查询日志
|
||||
mux.HandleFunc("/api/logs/stream", handleLogStream) // SSE 实时日志
|
||||
mux.HandleFunc("/api/logs/clear-module", api.CORS(handleClearLogsByModule)) // 按模块清除日志
|
||||
mux.HandleFunc("/api/logs/query", api.CORS(handleQueryLogs)) // 按模块查询日志
|
||||
mux.HandleFunc("/api/logs/stream", handleLogStream) // SSE 实时日志
|
||||
|
||||
// S2A 代理 API
|
||||
mux.HandleFunc("/api/s2a/test", api.CORS(handleS2ATest))
|
||||
@@ -296,6 +297,26 @@ func handleClearLogs(w http.ResponseWriter, r *http.Request) {
|
||||
api.Success(w, map[string]string{"message": "日志已清空"})
|
||||
}
|
||||
|
||||
// handleClearLogsByModule POST /api/logs/clear-module?module=cleaner
|
||||
func handleClearLogsByModule(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
api.Error(w, http.StatusMethodNotAllowed, "仅支持 POST")
|
||||
return
|
||||
}
|
||||
|
||||
module := r.URL.Query().Get("module")
|
||||
if module == "" {
|
||||
api.Error(w, http.StatusBadRequest, "缺少 module 参数")
|
||||
return
|
||||
}
|
||||
|
||||
cleared := logger.ClearLogsByModule(module)
|
||||
api.Success(w, map[string]interface{}{
|
||||
"message": fmt.Sprintf("已清除 %d 条 %s 日志", cleared, module),
|
||||
"cleared": cleared,
|
||||
})
|
||||
}
|
||||
|
||||
// handleQueryLogs GET /api/logs/query?module=cleaner&page=1&page_size=5
|
||||
func handleQueryLogs(w http.ResponseWriter, r *http.Request) {
|
||||
module := r.URL.Query().Get("module")
|
||||
|
||||
@@ -89,13 +89,10 @@ func checkAndCleanErrors() {
|
||||
}
|
||||
|
||||
if len(errorAccounts) == 0 {
|
||||
logger.Info("没有错误账号需要清理", "", "cleaner")
|
||||
lastCleanTime = time.Now()
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info(fmt.Sprintf("找到 %d 个错误账号,开始删除...", len(errorAccounts)), "", "cleaner")
|
||||
|
||||
success := 0
|
||||
failed := 0
|
||||
|
||||
@@ -106,7 +103,6 @@ func checkAndCleanErrors() {
|
||||
logger.Warning(fmt.Sprintf("删除账号失败: ID=%d, Email=%s, Error=%v", account.ID, account.Email, err), account.Email, "cleaner")
|
||||
} else {
|
||||
success++
|
||||
logger.Success(fmt.Sprintf("删除账号成功: ID=%d, Email=%s", account.ID, account.Email), account.Email, "cleaner")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -242,3 +242,22 @@ func ClearLogs() {
|
||||
defer logsMu.Unlock()
|
||||
logs = make([]LogEntry, 0, 1000)
|
||||
}
|
||||
|
||||
// ClearLogsByModule 按模块清除日志
|
||||
func ClearLogsByModule(module string) int {
|
||||
logsMu.Lock()
|
||||
defer logsMu.Unlock()
|
||||
|
||||
// 过滤掉指定模块的日志
|
||||
var newLogs []LogEntry
|
||||
cleared := 0
|
||||
for _, log := range logs {
|
||||
if log.Module != module {
|
||||
newLogs = append(newLogs, log)
|
||||
} else {
|
||||
cleared++
|
||||
}
|
||||
}
|
||||
logs = newLogs
|
||||
return cleared
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user