示例#1
0
func (b *Broker) HandleRequest(request *task.TaskRequest) error {
	var err error
	var str []byte

	now := time.Now().Unix()

	if request.StartTime == 0 {
		request.StartTime = now
	}

	if request.StartTime <= now {
		sdel := b.redisClient.HDel(config.RequestUuidSetTmp, request.Uuid)
		err = sdel.Err()
		if err != nil {
			log.BeegoLog.Error("Broker:HandleRequest:hdel error, err=%v, uuid=%v",
				err.Error(), request.Uuid)
			return err
		}

		err = b.AddRequestToRedis(request)
		if err != nil {
			return err
		}
	} else {
		str, err = json.Marshal(request)
		if err != nil {
			log.BeegoLog.Error("Broker:HandleRequest:json marshal error, err=%v",
				err.Error())
			return err
		}
		hset := b.redisClient.HSet(config.RequestUuidSetTmp, request.Uuid, string(str))
		err = hset.Err()
		if err != nil {
			log.BeegoLog.Error("Broker:HandleRequest:"+
				"put task into redis temp queue error,"+
				" err=%v, uuid=%v",
				err.Error(), request.Uuid)
			return err
		}

		afterTime := time.Second * time.Duration(request.StartTime-now)
		b.timer.NewTimer(afterTime, b.AddRequestToRedis, request)
	}

	return nil
}
示例#2
0
文件: web.go 项目: duanjiong/kingtask
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)
}
示例#3
0
文件: web.go 项目: duanjiong/kingtask
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)
}