From ab7019e0ec3788f82a69a61a4e1dd2cdb3023c9f Mon Sep 17 00:00:00 2001 From: WinfredLIN Date: Wed, 9 Oct 2024 14:51:03 +0800 Subject: [PATCH 1/3] api definition: add two param to workflow detail api --- sqle/api/controller/v2/workflow.go | 2 ++ sqle/docs/docs.go | 6 ++++++ sqle/docs/swagger.json | 6 ++++++ sqle/docs/swagger.yaml | 4 ++++ 4 files changed, 18 insertions(+) diff --git a/sqle/api/controller/v2/workflow.go b/sqle/api/controller/v2/workflow.go index 3229f4d9f..eb80730f2 100644 --- a/sqle/api/controller/v2/workflow.go +++ b/sqle/api/controller/v2/workflow.go @@ -1101,6 +1101,8 @@ type WorkflowRecordResV2 struct { Tasks []*WorkflowTaskItem `json:"tasks"` CurrentStepNumber uint `json:"current_step_number,omitempty"` Status string `json:"status" enums:"wait_for_audit,wait_for_execution,rejected,canceled,exec_failed,executing,finished"` + Executable bool `json:"executable"` + ExecutableReason string `json:"executable_reason"` Steps []*WorkflowStepResV2 `json:"workflow_step_list,omitempty"` } diff --git a/sqle/docs/docs.go b/sqle/docs/docs.go index 6df10d57e..a39b29eba 100644 --- a/sqle/docs/docs.go +++ b/sqle/docs/docs.go @@ -18323,6 +18323,12 @@ var doc = `{ "current_step_number": { "type": "integer" }, + "executable": { + "type": "boolean" + }, + "executable_reason": { + "type": "string" + }, "status": { "type": "string", "enum": [ diff --git a/sqle/docs/swagger.json b/sqle/docs/swagger.json index 036a476d1..03436c3d1 100644 --- a/sqle/docs/swagger.json +++ b/sqle/docs/swagger.json @@ -18307,6 +18307,12 @@ "current_step_number": { "type": "integer" }, + "executable": { + "type": "boolean" + }, + "executable_reason": { + "type": "string" + }, "status": { "type": "string", "enum": [ diff --git a/sqle/docs/swagger.yaml b/sqle/docs/swagger.yaml index 538f08e39..9bac8a91f 100644 --- a/sqle/docs/swagger.yaml +++ b/sqle/docs/swagger.yaml @@ -5379,6 +5379,10 @@ definitions: properties: current_step_number: type: integer + executable: + type: boolean + executable_reason: + type: string status: enum: - wait_for_audit From e77c201ca447421b8c0e6a625d46c465bda80a99 Mon Sep 17 00:00:00 2001 From: WinfredLIN Date: Wed, 9 Oct 2024 15:24:09 +0800 Subject: [PATCH 2/3] 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 +} From 38f9f1cecf1aecc9e264113147393969504d7d17 Mon Sep 17 00:00:00 2001 From: WinfredLIN Date: Thu, 10 Oct 2024 14:27:42 +0800 Subject: [PATCH 3/3] modify: add method to get workflow only by workflow id --- sqle/model/workflow.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sqle/model/workflow.go b/sqle/model/workflow.go index 5e79d33ab..1519d7c5d 100644 --- a/sqle/model/workflow.go +++ b/sqle/model/workflow.go @@ -921,6 +921,20 @@ func (s *Storage) GetWorkflowByProjectAndWorkflowId(projectId, workflowId string return workflow, true, errors.New(errors.ConnectStorageError, err) } +func (s *Storage) GetWorkflowByWorkflowId(workflowId string) (workflow *Workflow, exist bool, err error) { + err = s.db. + Preload("Record"). + Where("workflow_id = ?", workflowId). + First(&workflow).Error + if err != nil { + if err == gorm.ErrRecordNotFound { + return workflow, false, nil + } + return workflow, false, errors.New(errors.ConnectStorageError, err) + } + return workflow, true, nil +} + func (s *Storage) GetWorkflowExportById(workflowId string) (*Workflow, bool, error) { w := new(Workflow) err := s.db.Preload("Record").Where("workflow_id = ?", workflowId).First(&w).Error