Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: it is not allowed to execute workflow that have pre-workflow was not executed correctly #2651

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion sqle/api/controller/v2/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -1101,6 +1126,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"`
}

Expand Down Expand Up @@ -1266,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,
}
}

Expand Down
8 changes: 8 additions & 0 deletions sqle/api/controller/v3/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18323,6 +18323,12 @@ var doc = `{
"current_step_number": {
"type": "integer"
},
"executable": {
"type": "boolean"
},
"executable_reason": {
"type": "string"
},
"status": {
"type": "string",
"enum": [
Expand Down
6 changes: 6 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -18307,6 +18307,12 @@
"current_step_number": {
"type": "integer"
},
"executable": {
"type": "boolean"
},
"executable_reason": {
"type": "string"
},
"status": {
"type": "string",
"enum": [
Expand Down
4 changes: 4 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5379,6 +5379,10 @@ definitions:
properties:
current_step_number:
type: integer
executable:
type: boolean
executable_reason:
type: string
status:
enum:
- wait_for_audit
Expand Down
14 changes: 14 additions & 0 deletions sqle/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions sqle/server/sqlversion/sql_version_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading