func parseRpcResult(result *string, req *task.TaskRequest) error { var err error err = nil log.BeegoLog.Debug("Worker:DoRpcTaskRequest:result is %v", result) //prase json result //code 0 correct //code 1 incorrect type jsonResult struct { Code int `json:"code"` Wight int `json:"weight"` //重试权重 RetryInterval int `json:"retryInterval"` //重试间隔 } var tmp jsonResult err = json.Unmarshal([]byte(*result), &tmp) if err != nil { return err } if tmp.Code != rpcTaskSucess { err = goerror.New("Rpc error, " + *result) } if tmp.Wight < config.PriorityLow { req.Priority = config.PriorityDefault } else if tmp.Wight > config.PriorityHigh { req.Priority = config.PriorityDefault } else { req.Priority = tmp.Wight } if tmp.RetryInterval <= 0 { req.RetryInterval = config.RetryIntervalDefault } else { req.RetryInterval = tmp.RetryInterval } return err }
func (b *Broker) CreateRpcTaskRequest(c *echo.Context) error { args := struct { Method string `json:"method"` URL string `json:"url"` Args string `json:"args"` //json Marshal后的字符串 StartTime int64 `json:"start_time,string"` TimeInterval string `json:"time_interval"` //空格分隔各个参数 MaxRunTime int64 `json:"max_run_time,string"` Priority int `json:"priority,string"` MaxRetry int `json:"max_retry,string"` RetryInterval int `json:"retry_interval,string"` Uuid string `json:"uuid"` //如果用户指定uuid就使用它 }{} err := c.Bind(&args) if err != nil { log.BeegoLog.Error("Broker:CreateRpcTaskRequest:content-type is not json") return c.JSON(http.StatusForbidden, err.Error()) } if len(args.URL) == 0 { log.BeegoLog.Error("Broker:CreateRpcTaskRequest:url is %s", args.URL) return c.JSON(http.StatusForbidden, errors.ErrInvalidArgument.Error()) } taskRequest := new(task.TaskRequest) if args.Uuid == "" { taskRequest.Uuid = uuid.New() } else { taskRequest.Uuid = args.Uuid } taskRequest.BinName = args.URL taskRequest.Args = args.Args taskRequest.StartTime = args.StartTime taskRequest.TimeInterval = args.TimeInterval taskRequest.Index = 0 taskRequest.MaxRunTime = args.MaxRunTime if args.Priority < 1 { taskRequest.Priority = 1 } else if args.Priority > 10 { taskRequest.Priority = 10 } else { taskRequest.Priority = args.Priority } if args.MaxRetry >= 0 { taskRequest.MaxRetry = args.MaxRetry } else { taskRequest.MaxRetry = MAXRETRY } if args.RetryInterval > 0 { taskRequest.RetryInterval = args.RetryInterval } else { taskRequest.RetryInterval = -1 } switch args.Method { case "GET": taskRequest.TaskType = task.RpcTaskGET case "POST": taskRequest.TaskType = task.RpcTaskPOST case "PUT": taskRequest.TaskType = task.RpcTaskPUT case "DELETE": taskRequest.TaskType = task.RpcTaskDELETE default: return c.JSON(http.StatusForbidden, errors.ErrInvalidArgument.Error()) } err = b.HandleRequest(taskRequest) if err != nil { return c.JSON(http.StatusForbidden, err.Error()) } log.BeegoLog.Debug("Broker:CreateRpcTaskRequest:new task uuid is %s", taskRequest.Uuid) return c.JSON(http.StatusOK, taskRequest.Uuid) }
func (b *Broker) CreateScriptTaskRequest(c *echo.Context) error { args := struct { BinName string `json:"bin_name"` Args string `json:"args"` //空格分隔各个参数 StartTime int64 `json:"start_time,string"` TimeInterval string `json:"time_interval"` //空格分隔各个参数 MaxRunTime int64 `json:"max_run_time,string"` Priority int `json:"priority,string"` MaxRetry int `json:"max_retry,string"` RetryInterval int `json:"retry_interval,string"` Uuid string `json:"uuid"` }{} err := c.Bind(&args) if err != nil { log.BeegoLog.Error("Broker:CreateScriptTaskRequest:content-type is not json") return c.JSON(http.StatusForbidden, err.Error()) } if len(args.BinName) == 0 { return c.JSON(http.StatusForbidden, errors.ErrInvalidArgument.Error()) } taskRequest := new(task.TaskRequest) if args.Uuid == "" { taskRequest.Uuid = uuid.New() } else { taskRequest.Uuid = args.Uuid } taskRequest.BinName = args.BinName taskRequest.Args = args.Args taskRequest.StartTime = args.StartTime taskRequest.TimeInterval = args.TimeInterval taskRequest.Index = 0 taskRequest.MaxRunTime = args.MaxRunTime taskRequest.TaskType = task.ScriptTask if args.Priority < 1 { taskRequest.Priority = 1 } else if args.Priority > 10 { taskRequest.Priority = 10 } else { taskRequest.Priority = args.Priority } if args.MaxRetry >= 0 { taskRequest.MaxRetry = args.MaxRetry } else { taskRequest.MaxRetry = MAXRETRY } if args.RetryInterval > 0 { taskRequest.RetryInterval = args.RetryInterval } else { taskRequest.RetryInterval = -1 } err = b.HandleRequest(taskRequest) if err != nil { return c.JSON(http.StatusForbidden, err.Error()) } return c.JSON(http.StatusOK, taskRequest.Uuid) }