删除工作流时清理历史执行

pull/236/head
zhangchenhao 2025-06-06 11:52:43 +08:00
parent e31d5e2d04
commit 236076a031
3 changed files with 58 additions and 4 deletions

View File

@ -86,6 +86,11 @@ func DelWorkflow(id string) error {
if err != nil {
return err
}
// 清理工作流历史记录
err = CleanWorkflowHistory()
if err != nil {
return fmt.Errorf("清理工作流历史记录失败: %v", err)
}
return nil
}

View File

@ -4,6 +4,8 @@ import (
"ALLinSSL/backend/public"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
@ -114,3 +116,50 @@ func GetExecLog(id string) (string, error) {
}
return string(log), nil
}
func CleanWorkflowHistory() error {
s, err := GetSqlite()
if err != nil {
return err
}
defer s.Close()
// 获取所有工作流ID
data, err := s.Select()
if err != nil {
return err
}
var workflowIds []string
for _, v := range data {
if workflowId, ok := v["id"].(int64); ok {
workflowIds = append(workflowIds, strconv.FormatInt(workflowId, 10))
}
}
workflowIdsStr := strings.Join(workflowIds, ",")
s.TableName = "workflow_history"
// 获取无意义的工作流记录id
data, err = s.Where("workflow_id NOT IN ("+workflowIdsStr+")", nil).Select()
if err != nil {
return err
}
// 删除无意义的工作流记录
_, err = s.Where("workflow_id NOT IN ("+workflowIdsStr+")", nil).Delete()
if err != nil {
return err
}
// 删除工作流执行日志
logPath := public.GetSettingIgnoreError("workflow_log_path")
if logPath == "" {
logPath = "logs/workflow"
}
for _, v := range data {
if id, ok := v["id"].(string); ok && id != "" {
logFile := filepath.Join(logPath, id+".log")
if _, err := os.Stat(logFile); err == nil {
if err := os.Remove(logFile); err != nil {
return err
}
}
}
}
return nil
}

View File

@ -81,16 +81,16 @@ func Register(r *gin.Engine) {
}
// 1. 提供静态文件服务
r.StaticFS("/static", http.Dir("./frontend/static")) // 静态资源路径
r.StaticFS("/auto-deploy/static", http.Dir("./frontend/static")) // 静态资源路径
r.StaticFS("/static", http.Dir("./build/static")) // 静态资源路径
r.StaticFS("/auto-deploy/static", http.Dir("./build/static")) // 静态资源路径
// 返回 favicon.ico
r.GET("/favicon.ico", func(c *gin.Context) {
c.File("./frontend/favicon.ico")
c.File("./build/favicon.ico")
})
// 3. 前端路由托管:匹配所有其他路由并返回 index.html
r.NoRoute(func(c *gin.Context) {
c.File("./frontend/index.html")
c.File("./build/index.html")
})
// v2 := r.Group("/v2")
// {