175 lines
6.4 KiB
Go
175 lines
6.4 KiB
Go
package session
|
||
|
||
import (
|
||
flow "ai-agent/workflow/consts/flow"
|
||
"ai-agent/workflow/consts/public"
|
||
sessionDto "ai-agent/workflow/model/dto/session"
|
||
"ai-agent/workflow/model/entity"
|
||
"context"
|
||
"fmt"
|
||
"time"
|
||
|
||
"gitea.redpowerfuture.com/red-future/common/beans"
|
||
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
)
|
||
|
||
var ExecWorkflowDao = &execWorkflowDao{}
|
||
|
||
type execWorkflowDao struct{}
|
||
|
||
func (d *execWorkflowDao) Insert(ctx context.Context, req *sessionDto.CreateWorkflowReq) (id int64, err error) {
|
||
var s = new(entity.ExecWorkflow)
|
||
if err = gconv.Struct(req, &s); err != nil {
|
||
return
|
||
}
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).Insert(s)
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.LastInsertId()
|
||
}
|
||
|
||
func (d *execWorkflowDao) Delete(ctx context.Context, req *sessionDto.DeleteExecWorkflowReq) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).Where(entity.ExecWorkflowCol.Id, req.Id).Delete()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
func (d *execWorkflowDao) Update(ctx context.Context, req *sessionDto.UpdateWorkflowReq) (rows int64, err error) {
|
||
if req.Id <= 0 {
|
||
return
|
||
}
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).OmitEmpty().Data(&req).Where(entity.ExecWorkflowCol.Id, req.Id).Update()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
// ClearError 清空执行记录的报错信息(重新执行成功后调用,OmitEmpty 的 Update 会跳过空串,需显式写空)
|
||
func (d *execWorkflowDao) ClearError(ctx context.Context, id int64) (rows int64, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.Id, id).
|
||
Data(map[string]any{
|
||
entity.ExecWorkflowCol.ErrorMessage: "",
|
||
entity.ExecWorkflowCol.Error: "",
|
||
}).
|
||
Update()
|
||
if err != nil {
|
||
return
|
||
}
|
||
return r.RowsAffected()
|
||
}
|
||
|
||
func (d *execWorkflowDao) GetById(ctx context.Context, id int64) (res *entity.ExecWorkflow, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.Id, id).
|
||
One()
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Struct(&res)
|
||
return
|
||
}
|
||
|
||
func (d *execWorkflowDao) List(ctx context.Context, creator string, page *beans.Page) (res []*entity.ExecWorkflow, total int, err error) {
|
||
m := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.Creator, creator)
|
||
m.OrderDesc(entity.ExecWorkflowCol.CreatedAt)
|
||
if page != nil {
|
||
m.Page(int(page.PageNum), int(page.PageSize))
|
||
}
|
||
r, total, err := m.AllAndCount(false)
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Structs(&res)
|
||
return
|
||
}
|
||
|
||
// GetLatestBySessionAndFlow 查询会话+工作流下最近一次执行记录(按创建时间倒序,无记录返回 nil)
|
||
func (d *execWorkflowDao) GetLatestBySessionAndFlow(ctx context.Context, sessionId string, flowId int64) (res *entity.ExecWorkflow, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.SessionId, sessionId).
|
||
Where(entity.ExecWorkflowCol.FlowId, flowId).
|
||
OrderDesc(entity.ExecWorkflowCol.CreatedAt).
|
||
Limit(1).
|
||
One()
|
||
if err != nil {
|
||
return
|
||
}
|
||
if r.IsEmpty() {
|
||
return nil, nil
|
||
}
|
||
err = r.Struct(&res)
|
||
return
|
||
}
|
||
|
||
// ListBySession 查询会话下工作流执行记录(按创建时间倒序)
|
||
func (d *execWorkflowDao) ListBySession(ctx context.Context, sessionId string) (res []*entity.ExecWorkflow, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.SessionId, sessionId).
|
||
OrderDesc(entity.ExecWorkflowCol.CreatedAt).
|
||
All()
|
||
if err != nil {
|
||
return
|
||
}
|
||
err = r.Structs(&res)
|
||
return
|
||
}
|
||
|
||
// ResetRunning 置为运行中并刷新心跳(execute 复用失败记录 / reExecute / 恢复例程共用;
|
||
// 用 map 更新避免 OmitEmpty 省略 0 值,同时写 status、last_heartbeat、node_group_id)
|
||
func (d *execWorkflowDao) ResetRunning(ctx context.Context, id int64, nodeGroupId string) error {
|
||
_, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.Id, id).
|
||
Data(map[string]any{
|
||
entity.ExecWorkflowCol.Status: gconv.Int8(*flow.FlowExecutionStatusRunning.Code()),
|
||
entity.ExecWorkflowCol.LastHeartbeat: time.Now().UnixMilli(),
|
||
entity.ExecWorkflowCol.NodeGroupId: nodeGroupId,
|
||
}).
|
||
Update()
|
||
return err
|
||
}
|
||
|
||
// TouchHeartbeat 更新执行心跳(毫秒时间戳),供后台心跳 goroutine 每 30s 调用一次
|
||
func (d *execWorkflowDao) TouchHeartbeat(ctx context.Context, id int64) error {
|
||
_, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.Id, id).
|
||
Data(map[string]any{entity.ExecWorkflowCol.LastHeartbeat: time.Now().UnixMilli()}).
|
||
Update()
|
||
return err
|
||
}
|
||
|
||
// UpdateRetry 更新重试标记与已重试次数(map 更新,retryable=0 也需写入)
|
||
func (d *execWorkflowDao) UpdateRetry(ctx context.Context, id int64, retryable int, retryCount int) error {
|
||
_, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(entity.ExecWorkflowCol.Id, id).
|
||
Data(map[string]any{
|
||
entity.ExecWorkflowCol.Retryable: retryable,
|
||
entity.ExecWorkflowCol.RetryCount: retryCount,
|
||
}).
|
||
Update()
|
||
return err
|
||
}
|
||
|
||
// ListRecoverable 返回可恢复执行:僵尸运行中(status=1 且心跳陈旧)或可重试失败(status=3 且 retryable=1 且未耗尽)
|
||
func (d *execWorkflowDao) ListRecoverable(ctx context.Context, now int64, staleBefore int64, maxRetry int) (res []*entity.ExecWorkflow, err error) {
|
||
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecWorkflow).
|
||
Where(fmt.Sprintf("(%s = ? AND %s < ?) OR (%s = ? AND %s = 1 AND %s < ?)",
|
||
entity.ExecWorkflowCol.Status, entity.ExecWorkflowCol.LastHeartbeat,
|
||
entity.ExecWorkflowCol.Status, entity.ExecWorkflowCol.Retryable, entity.ExecWorkflowCol.RetryCount),
|
||
gconv.Int8(*flow.FlowExecutionStatusRunning.Code()), staleBefore,
|
||
gconv.Int8(*flow.FlowExecutionStatusFailed.Code()), maxRetry).
|
||
All()
|
||
if err != nil {
|
||
return
|
||
}
|
||
// 用 All+Structs 而非 Scan:Scan 生成的列清单会丢嵌入 SQLBaseDO 的 id 等基础列,恢复例程需要 id
|
||
err = r.Structs(&res)
|
||
return
|
||
}
|