Exemple #1
0
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)
}
Exemple #2
0
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)
}