Esempio n. 1
0
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
}
Esempio n. 2
0
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
}