mirror of
https://github.com/usual2970/certimate.git
synced 2025-07-28 05:58:34 +00:00
refactor: clean code
This commit is contained in:
@@ -65,9 +65,10 @@ func onWorkflowRecordCreateOrUpdate(ctx context.Context, record *core.Record) er
|
||||
|
||||
// 反之,重新添加定时任务
|
||||
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflowId), record.GetString("triggerCron"), func() {
|
||||
NewWorkflowService(repository.NewWorkflowRepository()).StartRun(ctx, &dtos.WorkflowStartRunReq{
|
||||
workflowSrv := NewWorkflowService(repository.NewWorkflowRepository(), repository.NewWorkflowRunRepository())
|
||||
workflowSrv.StartRun(ctx, &dtos.WorkflowStartRunReq{
|
||||
WorkflowId: workflowId,
|
||||
Trigger: domain.WorkflowTriggerTypeAuto,
|
||||
RunTrigger: domain.WorkflowTriggerTypeAuto,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
|
@@ -31,7 +31,7 @@ func NewApplyNode(node *domain.WorkflowNode) *applyNode {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *applyNode) Run(ctx context.Context) error {
|
||||
func (n *applyNode) Process(ctx context.Context) error {
|
||||
n.AddOutput(ctx, n.node.Name, "开始执行")
|
||||
|
||||
// 查询上次执行结果
|
||||
|
@@ -18,7 +18,7 @@ func NewConditionNode(node *domain.WorkflowNode) *conditionNode {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *conditionNode) Run(ctx context.Context) error {
|
||||
func (n *conditionNode) Process(ctx context.Context) error {
|
||||
// 此类型节点不需要执行任何操作,直接返回
|
||||
n.AddOutput(ctx, n.node.Name, "完成")
|
||||
|
||||
|
@@ -29,7 +29,7 @@ func NewDeployNode(node *domain.WorkflowNode) *deployNode {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *deployNode) Run(ctx context.Context) error {
|
||||
func (n *deployNode) Process(ctx context.Context) error {
|
||||
n.AddOutput(ctx, n.node.Name, "开始执行")
|
||||
|
||||
// 查询上次执行结果
|
||||
|
@@ -18,7 +18,7 @@ func NewExecuteFailureNode(node *domain.WorkflowNode) *executeFailureNode {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *executeFailureNode) Run(ctx context.Context) error {
|
||||
func (n *executeFailureNode) Process(ctx context.Context) error {
|
||||
// 此类型节点不需要执行任何操作,直接返回
|
||||
n.AddOutput(ctx, n.node.Name, "进入执行失败分支")
|
||||
|
||||
|
@@ -18,7 +18,7 @@ func NewExecuteSuccessNode(node *domain.WorkflowNode) *executeSuccessNode {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *executeSuccessNode) Run(ctx context.Context) error {
|
||||
func (n *executeSuccessNode) Process(ctx context.Context) error {
|
||||
// 此类型节点不需要执行任何操作,直接返回
|
||||
n.AddOutput(ctx, n.node.Name, "进入执行成功分支")
|
||||
|
||||
|
@@ -24,7 +24,7 @@ func NewNotifyNode(node *domain.WorkflowNode) *notifyNode {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *notifyNode) Run(ctx context.Context) error {
|
||||
func (n *notifyNode) Process(ctx context.Context) error {
|
||||
n.AddOutput(ctx, n.node.Name, "开始执行")
|
||||
|
||||
nodeConfig := n.node.GetConfigForNotify()
|
||||
|
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
type NodeProcessor interface {
|
||||
Run(ctx context.Context) error
|
||||
Process(ctx context.Context) error
|
||||
GetLog(ctx context.Context) *domain.WorkflowRunLog
|
||||
AddOutput(ctx context.Context, title, content string, err ...string)
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ func NewStartNode(node *domain.WorkflowNode) *startNode {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *startNode) Run(ctx context.Context) error {
|
||||
func (n *startNode) Process(ctx context.Context) error {
|
||||
// 此类型节点不需要执行任何操作,直接返回
|
||||
n.AddOutput(ctx, n.node.Name, "完成")
|
||||
|
||||
|
@@ -29,9 +29,7 @@ func NewUploadNode(node *domain.WorkflowNode) *uploadNode {
|
||||
}
|
||||
}
|
||||
|
||||
// Run 上传证书节点执行
|
||||
// 包含上传证书的工作流,理论上应该手动执行,如果每天定时执行,也只是重新保存一下
|
||||
func (n *uploadNode) Run(ctx context.Context) error {
|
||||
func (n *uploadNode) Process(ctx context.Context) error {
|
||||
n.AddOutput(ctx, n.node.Name, "进入上传证书节点")
|
||||
|
||||
nodeConfig := n.node.GetConfigForUpload()
|
||||
|
@@ -8,27 +8,29 @@ import (
|
||||
)
|
||||
|
||||
type workflowProcessor struct {
|
||||
workflow *domain.Workflow
|
||||
workflowRun *domain.WorkflowRun
|
||||
workflorRunLogs []domain.WorkflowRunLog
|
||||
workflowId string
|
||||
workflowContent *domain.WorkflowNode
|
||||
runId string
|
||||
runLogs []domain.WorkflowRunLog
|
||||
}
|
||||
|
||||
func NewWorkflowProcessor(workflow *domain.Workflow, workflowRun *domain.WorkflowRun) *workflowProcessor {
|
||||
func NewWorkflowProcessor(workflowId string, workflowContent *domain.WorkflowNode, workflowRunId string) *workflowProcessor {
|
||||
return &workflowProcessor{
|
||||
workflow: workflow,
|
||||
workflowRun: workflowRun,
|
||||
workflorRunLogs: make([]domain.WorkflowRunLog, 0),
|
||||
workflowId: workflowId,
|
||||
workflowContent: workflowContent,
|
||||
runId: workflowRunId,
|
||||
runLogs: make([]domain.WorkflowRunLog, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *workflowProcessor) Run(ctx context.Context) error {
|
||||
ctx = context.WithValue(ctx, "workflow_id", w.workflow.Id)
|
||||
ctx = context.WithValue(ctx, "workflow_run_id", w.workflowRun.Id)
|
||||
return w.processNode(ctx, w.workflow.Content)
|
||||
func (w *workflowProcessor) Process(ctx context.Context) error {
|
||||
ctx = context.WithValue(ctx, "workflow_id", w.workflowId)
|
||||
ctx = context.WithValue(ctx, "workflow_run_id", w.runId)
|
||||
return w.processNode(ctx, w.workflowContent)
|
||||
}
|
||||
|
||||
func (w *workflowProcessor) GetRunLogs() []domain.WorkflowRunLog {
|
||||
return w.workflorRunLogs
|
||||
func (w *workflowProcessor) GetLogs() []domain.WorkflowRunLog {
|
||||
return w.runLogs
|
||||
}
|
||||
|
||||
func (w *workflowProcessor) processNode(ctx context.Context, node *domain.WorkflowNode) error {
|
||||
@@ -51,10 +53,10 @@ func (w *workflowProcessor) processNode(ctx context.Context, node *domain.Workfl
|
||||
break
|
||||
}
|
||||
|
||||
runErr = processor.Run(ctx)
|
||||
runErr = processor.Process(ctx)
|
||||
log := processor.GetLog(ctx)
|
||||
if log != nil {
|
||||
w.workflorRunLogs = append(w.workflorRunLogs, *log)
|
||||
w.runLogs = append(w.runLogs, *log)
|
||||
}
|
||||
if runErr != nil {
|
||||
break
|
||||
@@ -67,9 +69,9 @@ func (w *workflowProcessor) processNode(ctx context.Context, node *domain.Workfl
|
||||
if runErr != nil && current.Next != nil && current.Next.Type != domain.WorkflowNodeTypeExecuteResultBranch {
|
||||
return runErr
|
||||
} else if runErr != nil && current.Next != nil && current.Next.Type == domain.WorkflowNodeTypeExecuteResultBranch {
|
||||
current = getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteFailure)
|
||||
current = w.getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteFailure)
|
||||
} else if runErr == nil && current.Next != nil && current.Next.Type == domain.WorkflowNodeTypeExecuteResultBranch {
|
||||
current = getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteSuccess)
|
||||
current = w.getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteSuccess)
|
||||
} else {
|
||||
current = current.Next
|
||||
}
|
||||
@@ -78,7 +80,7 @@ func (w *workflowProcessor) processNode(ctx context.Context, node *domain.Workfl
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBranchByType(branches []domain.WorkflowNode, nodeType domain.WorkflowNodeType) *domain.WorkflowNode {
|
||||
func (w *workflowProcessor) getBranchByType(branches []domain.WorkflowNode, nodeType domain.WorkflowNodeType) *domain.WorkflowNode {
|
||||
for _, branch := range branches {
|
||||
if branch.Type == nodeType {
|
||||
return &branch
|
||||
|
@@ -13,46 +13,55 @@ import (
|
||||
processor "github.com/usual2970/certimate/internal/workflow/processor"
|
||||
)
|
||||
|
||||
const defaultRoutines = 10
|
||||
const defaultRoutines = 16
|
||||
|
||||
type workflowRunData struct {
|
||||
Workflow *domain.Workflow
|
||||
RunTrigger domain.WorkflowTriggerType
|
||||
WorkflowId string
|
||||
WorkflowContent *domain.WorkflowNode
|
||||
RunTrigger domain.WorkflowTriggerType
|
||||
}
|
||||
|
||||
type workflowRepository interface {
|
||||
ListEnabledAuto(ctx context.Context) ([]*domain.Workflow, error)
|
||||
GetById(ctx context.Context, id string) (*domain.Workflow, error)
|
||||
Save(ctx context.Context, workflow *domain.Workflow) (*domain.Workflow, error)
|
||||
SaveRun(ctx context.Context, workflowRun *domain.WorkflowRun) (*domain.WorkflowRun, error)
|
||||
}
|
||||
|
||||
type workflowRunRepository interface {
|
||||
GetById(ctx context.Context, id string) (*domain.WorkflowRun, error)
|
||||
Save(ctx context.Context, workflowRun *domain.WorkflowRun) (*domain.WorkflowRun, error)
|
||||
}
|
||||
|
||||
type WorkflowService struct {
|
||||
ch chan *workflowRunData
|
||||
repo workflowRepository
|
||||
wg sync.WaitGroup
|
||||
cancel context.CancelFunc
|
||||
|
||||
workflowRepo workflowRepository
|
||||
workflowRunRepo workflowRunRepository
|
||||
}
|
||||
|
||||
func NewWorkflowService(repo workflowRepository) *WorkflowService {
|
||||
srv := &WorkflowService{
|
||||
repo: repo,
|
||||
ch: make(chan *workflowRunData, 1),
|
||||
}
|
||||
|
||||
func NewWorkflowService(workflowRepo workflowRepository, workflowRunRepo workflowRunRepository) *WorkflowService {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
srv.cancel = cancel
|
||||
|
||||
srv := &WorkflowService{
|
||||
ch: make(chan *workflowRunData, 1),
|
||||
cancel: cancel,
|
||||
|
||||
workflowRepo: workflowRepo,
|
||||
workflowRunRepo: workflowRunRepo,
|
||||
}
|
||||
|
||||
srv.wg.Add(defaultRoutines)
|
||||
for i := 0; i < defaultRoutines; i++ {
|
||||
go srv.run(ctx)
|
||||
go srv.startRun(ctx)
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
func (s *WorkflowService) InitSchedule(ctx context.Context) error {
|
||||
workflows, err := s.repo.ListEnabledAuto(ctx)
|
||||
workflows, err := s.workflowRepo.ListEnabledAuto(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -62,7 +71,7 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
|
||||
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflow.Id), workflow.TriggerCron, func() {
|
||||
s.StartRun(ctx, &dtos.WorkflowStartRunReq{
|
||||
WorkflowId: workflow.Id,
|
||||
Trigger: domain.WorkflowTriggerTypeAuto,
|
||||
RunTrigger: domain.WorkflowTriggerTypeAuto,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
@@ -75,35 +84,50 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (s *WorkflowService) StartRun(ctx context.Context, req *dtos.WorkflowStartRunReq) error {
|
||||
workflow, err := s.repo.GetById(ctx, req.WorkflowId)
|
||||
workflow, err := s.workflowRepo.GetById(ctx, req.WorkflowId)
|
||||
if err != nil {
|
||||
app.GetLogger().Error("failed to get workflow", "id", req.WorkflowId, "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if workflow.LastRunStatus == domain.WorkflowRunStatusTypeRunning {
|
||||
return errors.New("workflow is running")
|
||||
}
|
||||
|
||||
workflow.LastRunTime = time.Now()
|
||||
workflow.LastRunStatus = domain.WorkflowRunStatusTypePending
|
||||
workflow.LastRunId = ""
|
||||
if resp, err := s.repo.Save(ctx, workflow); err != nil {
|
||||
return err
|
||||
} else {
|
||||
workflow = resp
|
||||
if workflow.LastRunStatus == domain.WorkflowRunStatusTypePending || workflow.LastRunStatus == domain.WorkflowRunStatusTypeRunning {
|
||||
return errors.New("workflow is already pending or running")
|
||||
}
|
||||
|
||||
s.ch <- &workflowRunData{
|
||||
Workflow: workflow,
|
||||
RunTrigger: req.Trigger,
|
||||
WorkflowId: workflow.Id,
|
||||
WorkflowContent: workflow.Content,
|
||||
RunTrigger: req.RunTrigger,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *WorkflowService) CancelRun(ctx context.Context, req *dtos.WorkflowCancelRunReq) error {
|
||||
workflow, err := s.workflowRepo.GetById(ctx, req.WorkflowId)
|
||||
if err != nil {
|
||||
app.GetLogger().Error("failed to get workflow", "id", req.WorkflowId, "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
workflowRun, err := s.workflowRunRepo.GetById(ctx, req.RunId)
|
||||
if err != nil {
|
||||
app.GetLogger().Error("failed to get workflow run", "id", req.RunId, "err", err)
|
||||
return err
|
||||
} else if workflowRun.WorkflowId != workflow.Id {
|
||||
return errors.New("workflow run not found")
|
||||
} else if workflowRun.Status != domain.WorkflowRunStatusTypePending && workflowRun.Status != domain.WorkflowRunStatusTypeRunning {
|
||||
return errors.New("workflow run is not pending or running")
|
||||
}
|
||||
|
||||
// TODO: 取消运行,防止因为某些原因意外挂起(如进程被杀死)导致工作流一直处于 running 状态无法重新运行
|
||||
// workflowRun.Status = domain.WorkflowRunStatusTypeCanceled
|
||||
// workflowRun.EndedAt = time.Now()
|
||||
// if _, err := s.workflowRunRepo.Save(ctx, workflowRun); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// return nil
|
||||
|
||||
return errors.New("TODO: 尚未实现")
|
||||
}
|
||||
@@ -113,13 +137,14 @@ func (s *WorkflowService) Stop(ctx context.Context) {
|
||||
s.wg.Wait()
|
||||
}
|
||||
|
||||
func (s *WorkflowService) run(ctx context.Context) {
|
||||
func (s *WorkflowService) startRun(ctx context.Context) {
|
||||
defer s.wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case data := <-s.ch:
|
||||
if err := s.runWithData(ctx, data); err != nil {
|
||||
app.GetLogger().Error("failed to run workflow", "id", data.Workflow.Id, "err", err)
|
||||
if err := s.startRunWithData(ctx, data); err != nil {
|
||||
app.GetLogger().Error("failed to run workflow", "id", data.WorkflowId, "err", err)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return
|
||||
@@ -127,27 +152,26 @@ func (s *WorkflowService) run(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WorkflowService) runWithData(ctx context.Context, runData *workflowRunData) error {
|
||||
workflow := runData.Workflow
|
||||
func (s *WorkflowService) startRunWithData(ctx context.Context, data *workflowRunData) error {
|
||||
run := &domain.WorkflowRun{
|
||||
WorkflowId: workflow.Id,
|
||||
WorkflowId: data.WorkflowId,
|
||||
Status: domain.WorkflowRunStatusTypeRunning,
|
||||
Trigger: runData.RunTrigger,
|
||||
Trigger: data.RunTrigger,
|
||||
StartedAt: time.Now(),
|
||||
}
|
||||
if resp, err := s.repo.SaveRun(ctx, run); err != nil {
|
||||
if resp, err := s.workflowRunRepo.Save(ctx, run); err != nil {
|
||||
return err
|
||||
} else {
|
||||
run = resp
|
||||
}
|
||||
|
||||
processor := processor.NewWorkflowProcessor(workflow, run)
|
||||
if runErr := processor.Run(ctx); runErr != nil {
|
||||
processor := processor.NewWorkflowProcessor(data.WorkflowId, data.WorkflowContent, run.Id)
|
||||
if runErr := processor.Process(ctx); runErr != nil {
|
||||
run.Status = domain.WorkflowRunStatusTypeFailed
|
||||
run.EndedAt = time.Now()
|
||||
run.Logs = processor.GetRunLogs()
|
||||
run.Logs = processor.GetLogs()
|
||||
run.Error = runErr.Error()
|
||||
if _, err := s.repo.SaveRun(ctx, run); err != nil {
|
||||
if _, err := s.workflowRunRepo.Save(ctx, run); err != nil {
|
||||
app.GetLogger().Error("failed to save workflow run", "err", err)
|
||||
}
|
||||
|
||||
@@ -155,14 +179,14 @@ func (s *WorkflowService) runWithData(ctx context.Context, runData *workflowRunD
|
||||
}
|
||||
|
||||
run.EndedAt = time.Now()
|
||||
run.Logs = processor.GetRunLogs()
|
||||
run.Logs = processor.GetLogs()
|
||||
run.Error = domain.WorkflowRunLogs(run.Logs).ErrorString()
|
||||
if run.Error == "" {
|
||||
run.Status = domain.WorkflowRunStatusTypeSucceeded
|
||||
} else {
|
||||
run.Status = domain.WorkflowRunStatusTypeFailed
|
||||
}
|
||||
if _, err := s.repo.SaveRun(ctx, run); err != nil {
|
||||
if _, err := s.workflowRunRepo.Save(ctx, run); err != nil {
|
||||
app.GetLogger().Error("failed to save workflow run", "err", err)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user