From e77c201ca447421b8c0e6a625d46c465bda80a99 Mon Sep 17 00:00:00 2001 From: WinfredLIN Date: Wed, 9 Oct 2024 15:24:09 +0800 Subject: [PATCH] feat: it is not allowed to execute workflow that have pre-workflow was not executed correctly --- sqle/api/controller/v2/workflow.go | 39 +++++++++++++++++++++++- sqle/api/controller/v3/workflow.go | 8 +++++ sqle/server/sqlversion/sql_version_ce.go | 4 +++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/sqle/api/controller/v2/workflow.go b/sqle/api/controller/v2/workflow.go index eb80730f2..15a59841b 100644 --- a/sqle/api/controller/v2/workflow.go +++ b/sqle/api/controller/v2/workflow.go @@ -434,6 +434,14 @@ func ExecuteOneTaskOnWorkflowV2(c echo.Context) error { return controller.JSONBaseErrorReq(c, err) } + executable, reason, err := sqlversion.CheckWorkflowExecutable(projectUid, workflowID) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + if !executable { + return controller.JSONBaseErrorReq(c, fmt.Errorf(reason)) + } + isCan, err := v1.IsTaskCanExecute(s, taskIdStr) if err != nil { return controller.JSONBaseErrorReq(c, err) @@ -1027,6 +1035,14 @@ func UpdateWorkflowScheduleV2(c echo.Context) error { return controller.JSONBaseErrorReq(c, v1.ErrWorkflowExecuteTimeIncorrect) } + executable, reason, err := sqlversion.CheckWorkflowExecutable(projectUid, workflowId) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + if !executable { + return controller.JSONBaseErrorReq(c, fmt.Errorf(reason)) + } + err = s.UpdateInstanceRecordSchedule(curTaskRecord, user.GetIDStr(), req.ScheduleTime) if err != nil { return controller.JSONBaseErrorReq(c, err) @@ -1074,8 +1090,17 @@ func ExecuteTasksOnWorkflowV2(c echo.Context) error { if err != nil { return controller.JSONBaseErrorReq(c, err) } + + executable, reason, err := sqlversion.CheckWorkflowExecutable(projectUid, workflowId) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + if !executable { + return controller.JSONBaseErrorReq(c, fmt.Errorf(reason)) + } + if err := v1.PrepareForWorkflowExecution(c, projectUid, workflow, user); err != nil { - return err + return controller.JSONBaseErrorReq(c, err) } _, err = server.ExecuteTasksProcess(workflow.WorkflowId, projectUid, user) @@ -1268,11 +1293,23 @@ func convertWorkflowRecordToRes(workflow *model.Workflow, record *model.Workflow tasksRes[i] = &WorkflowTaskItem{Id: inst.TaskId} } + var err error + var executable bool + var reason string = fmt.Sprintf("the status of workflow is %v", record.Status) + if record.Status == model.WorkflowStatusWaitForExecution { + executable, reason, err = sqlversion.CheckWorkflowExecutable(string(workflow.ProjectId), workflow.WorkflowId) + if err != nil { + reason = err.Error() + } + } + return &WorkflowRecordResV2{ Tasks: tasksRes, CurrentStepNumber: currentStepNum, Status: record.Status, Steps: steps, + Executable: executable, + ExecutableReason: reason, } } diff --git a/sqle/api/controller/v3/workflow.go b/sqle/api/controller/v3/workflow.go index b53bfcc48..1a877c581 100644 --- a/sqle/api/controller/v3/workflow.go +++ b/sqle/api/controller/v3/workflow.go @@ -10,6 +10,7 @@ import ( "github.com/actiontech/sqle/sqle/dms" "github.com/actiontech/sqle/sqle/errors" "github.com/actiontech/sqle/sqle/model" + "github.com/actiontech/sqle/sqle/server/sqlversion" "github.com/labstack/echo/v4" ) @@ -54,6 +55,13 @@ func BatchCompleteWorkflowsV3(c echo.Context) error { s := model.GetStorage() workflows := make([]*model.Workflow, len(req.WorkflowList)) for i, completeWorkflow := range req.WorkflowList { + executable, reason, err := sqlversion.CheckWorkflowExecutable(projectUid, completeWorkflow.WorkflowID) + if err != nil { + return controller.JSONBaseErrorReq(c, err) + } + if !executable { + return controller.JSONBaseErrorReq(c, fmt.Errorf(reason)) + } workflow, err := v2.CheckCanCompleteWorkflow(projectUid, completeWorkflow.WorkflowID) if err != nil { return controller.JSONBaseErrorReq(c, err) diff --git a/sqle/server/sqlversion/sql_version_ce.go b/sqle/server/sqlversion/sql_version_ce.go index 052a70487..9507d025d 100644 --- a/sqle/server/sqlversion/sql_version_ce.go +++ b/sqle/server/sqlversion/sql_version_ce.go @@ -12,3 +12,7 @@ import ( func CheckInstanceInWorkflowCanAssociateToTheFirstStageOfVersion(versionID uint, instanceId []uint64) error { return errors.New(errors.EnterpriseEditionFeatures, e.New("sql version is enterprise version feature")) } + +func CheckWorkflowExecutable(projectUid, workflowId string) (executable bool, reason string, err error) { + return true, "", nil +}