コード例 #1
0
ファイル: broker.go プロジェクト: NotBadPad/kingtask
func (b *Broker) HandleRequest(rb *bufio.Reader, c net.Conn) error {
	buf := make([]byte, 1024)
	request := new(task.TaskRequest)
	readLen, err := rb.Read(buf)
	if err != nil {
		b.WriteError(err, c)
		return err
	}

	err = json.Unmarshal(buf[:readLen], request)
	if err != nil {
		b.WriteError(err, c)
		return err
	}

	now := time.Now().Unix()
	if request.StartTime == 0 {
		request.StartTime = now
	}

	if request.StartTime <= now {
		err = b.AddRequestToRedis(request)
		if err != nil {
			b.WriteError(err, c)
			return err
		}
	} else {
		afterTime := time.Second * time.Duration(request.StartTime-now)
		b.timer.NewTimer(afterTime, b.AddRequestToRedis, request)
	}

	return b.WriteOK(c)
}
コード例 #2
0
ファイル: broker.go プロジェクト: xxoxx/kingtask
func (b *Broker) resetTaskRequest(args []interface{}) error {
	var err error
	if len(args) == 0 || len(args) != config.TaskRequestItemCount {
		return errors.ErrInvalidArgument
	}
	request := new(task.TaskRequest)
	request.Uuid = args[0].(string)
	request.BinName = args[1].(string)
	request.Args = args[2].(string)
	request.StartTime, err = strconv.ParseInt(args[3].(string), 10, 64)
	if err != nil {
		return err
	}
	request.TimeInterval = args[4].(string)
	request.Index, err = strconv.Atoi(args[5].(string))
	if err != nil {
		return err
	}
	vec := strings.Split(request.TimeInterval, " ")
	request.Index++
	if request.Index < len(vec) {
		timeLater, err := strconv.Atoi(vec[request.Index])
		if err != nil {
			return err
		}
		afterTime := time.Second * time.Duration(timeLater)
		b.timer.NewTimer(afterTime, b.AddRequestToRedis, request)
	} else {
		golog.Error("Broker", "HandleFailTask", "retry max time", 0,
			"key", fmt.Sprintf("t_%s", request.Uuid))
		return errors.ErrTryMaxTimes
	}
	return nil
}
コード例 #3
0
ファイル: worker.go プロジェクト: trigrass2/kingtask
func (w *Worker) DoTaskRequest(args []interface{}) (*task.TaskResult, error) {
	var err error
	var output string

	req := new(task.TaskRequest)
	ret := new(task.TaskResult)

	req.Uuid = args[0].(string)
	req.BinName = args[1].(string)
	req.Args = args[2].(string)
	req.StartTime, err = strconv.ParseInt(args[3].(string), 10, 64)
	if err != nil {
		return nil, err
	}
	req.TimeInterval = args[4].(string)
	req.Index, err = strconv.Atoi(args[5].(string))
	if err != nil {
		return nil, err
	}
	req.MaxRunTime, err = strconv.ParseInt(args[6].(string), 10, 64)
	if err != nil {
		return nil, err
	}
	req.TaskType, err = strconv.Atoi(args[7].(string))
	if err != nil {
		return nil, err
	}
	switch req.TaskType {
	case task.ScriptTask:
		output, err = w.DoScriptTaskRequest(req)
	case task.RpcTaskGET, task.RpcTaskPOST, task.RpcTaskPUT, task.RpcTaskDELETE:
		output, err = w.DoRpcTaskRequest(req)
	default:
		err = errors.ErrInvalidArgument
		golog.Error("Worker", "DoTaskRequest", "task type error", 0, "task_type", req.TaskType)
	}
	ret.TaskRequest = *req
	//执行任务失败,
	if err != nil {
		ret.IsSuccess = int64(0)
		ret.Result = err.Error()
		return ret, nil
	}
	ret.IsSuccess = int64(1)
	ret.Result = output

	return ret, nil
}
コード例 #4
0
ファイル: worker.go プロジェクト: NotBadPad/kingtask
func (w *Worker) DoTaskRequest(args []interface{}) (*task.TaskResult, error) {
	var err error
	var output string
	req := new(task.TaskRequest)
	ret := new(task.TaskResult)

	req.Uuid = args[0].(string)
	req.BinName = args[1].(string)
	req.Args = args[2].(string)
	req.StartTime, err = strconv.ParseInt(args[3].(string), 10, 64)
	if err != nil {
		return nil, err
	}
	req.TimeInterval = args[4].(string)
	req.Index, err = strconv.Atoi(args[5].(string))
	if err != nil {
		return nil, err
	}
	binPath := path.Clean(w.cfg.BinPath + "/" + req.BinName)
	_, err = os.Stat(binPath)
	if err != nil && os.IsNotExist(err) {
		golog.Error("worker", "DoTaskRequest", "File not exist", 0,
			"key", fmt.Sprintf("t_%s", req.Uuid),
			"bin_path", binPath,
		)
		return nil, errors.ErrFileNotExist
	}
	if len(req.Args) == 0 {
		output, err = w.ExecBin(binPath, nil)
	} else {
		argsVec := strings.Split(req.Args, " ")
		output, err = w.ExecBin(binPath, argsVec)
	}

	ret.TaskRequest = *req
	//执行任务失败
	if err != nil {
		ret.IsSuccess = int64(0)
		ret.Result = err.Error()
		return ret, nil
	}
	ret.IsSuccess = int64(1)
	ret.Result = output

	return ret, nil
}
コード例 #5
0
ファイル: web.go プロジェクト: trigrass2/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"`
	}{}

	err := c.Bind(&args)
	if err != nil {
		return c.JSON(http.StatusForbidden, err.Error())
	}
	taskRequest := new(task.TaskRequest)
	taskRequest.Uuid = uuid.New()
	if len(args.BinName) == 0 {
		return c.JSON(http.StatusForbidden, errors.ErrInvalidArgument.Error())
	}

	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

	err = b.HandleRequest(taskRequest)
	if err != nil {
		return c.JSON(http.StatusForbidden, err.Error())
	}
	golog.Info("Broker", "CreateScriptTaskRequest", "ok", 0,
		"uuid", taskRequest.Uuid,
		"bin_name", taskRequest.BinName,
		"args", taskRequest.Args,
		"start_time", taskRequest.StartTime,
		"time_interval", taskRequest.TimeInterval,
		"index", taskRequest.Index,
		"max_run_time", taskRequest.MaxRunTime,
		"task_type", taskRequest.TaskType,
	)
	return c.JSON(http.StatusOK, taskRequest.Uuid)
}
コード例 #6
0
ファイル: broker.go プロジェクト: xxoxx/kingtask
func (b *Broker) HandleRequest(request *task.TaskRequest) error {
	var err error
	now := time.Now().Unix()
	if request.StartTime == 0 {
		request.StartTime = now
	}

	if request.StartTime <= now {
		err = b.AddRequestToRedis(request)
		if err != nil {
			return err
		}
	} else {
		afterTime := time.Second * time.Duration(request.StartTime-now)
		b.timer.NewTimer(afterTime, b.AddRequestToRedis, request)
	}

	return nil
}
コード例 #7
0
ファイル: web.go プロジェクト: trigrass2/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"`
	}{}

	err := c.Bind(&args)
	if err != nil {
		return c.JSON(http.StatusForbidden, err.Error())
	}
	fmt.Println(args)
	taskRequest := new(task.TaskRequest)
	taskRequest.Uuid = uuid.New()
	if len(args.URL) == 0 {
		return c.JSON(http.StatusForbidden, errors.ErrInvalidArgument.Error())
	}

	taskRequest.BinName = args.URL
	taskRequest.Args = args.Args
	taskRequest.StartTime = args.StartTime
	taskRequest.TimeInterval = args.TimeInterval
	taskRequest.Index = 0
	taskRequest.MaxRunTime = args.MaxRunTime
	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())
	}
	golog.Info("Broker", "CreateRpcTaskRequest", "ok", 0,
		"uuid", taskRequest.Uuid,
		"bin_name", taskRequest.BinName,
		"args", taskRequest.Args,
		"start_time", taskRequest.StartTime,
		"time_interval", taskRequest.TimeInterval,
		"index", taskRequest.Index,
		"max_run_time", taskRequest.MaxRunTime,
		"task_type", taskRequest.TaskType,
	)
	return c.JSON(http.StatusOK, taskRequest.Uuid)
}