func checkAndRollbackWorkflow(hostId string, operator string) error { // 1.查询流程状态 /* 请求接口 */ res, err := common.Request(map[string]interface{}{ "url": config.GetUCAApi("delivery", "workflow_list") + "?current=1&size=500&masterResourceId=" + hostId, "method": "GET", "time_out": time.Second * 15, }) if err != nil { common.Log.Errorf("系统错误:查询节点%s关联流程详情接口失败:%v", hostId, err) return errors.New(code.ErrorRollbackWorkflow) } if res["Status"] != "Success" { // 接口出错了 common.Log.Errorf("查询节点%s关联流程详情接口失败,resp: %s", hostId, res) return errors.New(code.ErrorRollbackWorkflow) } workFlows := res["Detail"].(map[string]interface{})["Records"].([]interface{}) for _, workFlow := range workFlows { workExecState := workFlow.(map[string]interface{})["WorkExecState"] if workExecState == "completed" || workExecState == "repaired" { continue } if workExecState == "processing" { common.Log.Errorf("该节点%s关联流程%s未结束,resp: %s", hostId, workFlow.(map[string]interface{})["WorkFlowId"], res) return errors.New(code.ErrorRollbackWorkflow) } if workExecState == "failed" { // 2.执行回滚 WorkFlowId,Operator err := rollbackWorkflow(workFlow.(map[string]interface{})["WorkFlowId"].(string), operator) if err != nil { common.Log.Errorf("回滚节点%s关联流程失败,resp: %s", hostId, res) return errors.New(code.ErrorRollbackWorkflow) } } } return nil } 优化这段代码
时间: 2023-07-19 12:06:39 浏览: 246
godoc https://godoc.org/github.com/mnhkahn/gogogo-Golang开发
这段代码可以进行如下优化:
1. 提取常量和变量,使代码更易读:
```
const (
CompletedState = "completed"
RepairedState = "repaired"
ProcessingState = "processing"
FailedState = "failed"
)
type WorkFlow struct {
WorkFlowId string `json:"WorkFlowId"`
WorkExecState string `json:"WorkExecState"`
}
```
2. 将请求接口的逻辑封装为函数,提高代码复用性:
```
func getWorkFlows(hostId string) ([]WorkFlow, error) {
res, err := common.Request(map[string]interface{}{
"url": config.GetUCAApi("delivery", "workflow_list") + "?current=1&size=500&masterResourceId=" + hostId,
"method": "GET",
"time_out": time.Second * 15,
})
if err != nil {
common.Log.Errorf("系统错误:查询节点%s关联流程详情接口失败:%v", hostId, err)
return nil, errors.New(code.ErrorRollbackWorkflow)
}
if res["Status"] != "Success" {
// 接口出错了
common.Log.Errorf("查询节点%s关联流程详情接口失败,resp: %s", hostId, res)
return nil, errors.New(code.ErrorRollbackWorkflow)
}
workFlows := []WorkFlow{}
for _, record := range res["Detail"].(map[string]interface{})["Records"].([]interface{}) {
workFlow := WorkFlow{
WorkFlowId: record.(map[string]interface{})["WorkFlowId"].(string),
WorkExecState: record.(map[string]interface{})["WorkExecState"].(string),
}
workFlows = append(workFlows, workFlow)
}
return workFlows, nil
}
```
3. 使用 switch 替代 if-else 简化代码:
```
func checkAndRollbackWorkflow(hostId string, operator string) error {
workFlows, err := getWorkFlows(hostId)
if err != nil {
return err
}
for _, workFlow := range workFlows {
switch workFlow.WorkExecState {
case CompletedState, RepairedState:
continue
case ProcessingState:
common.Log.Errorf("该节点%s关联流程%s未结束,resp: %s", hostId, workFlow.WorkFlowId, res)
return errors.New(code.ErrorRollbackWorkflow)
case FailedState:
if err := rollbackWorkflow(workFlow.WorkFlowId, operator); err != nil {
common.Log.Errorf("回滚节点%s关联流程失败,resp: %s", hostId, res)
return errors.New(code.ErrorRollbackWorkflow)
}
}
}
return nil
}
```
阅读全文