예제 #1
0
func (w *Worker) callRpc(req *http.Request, maxRunTime time.Duration) (string, error) {
	var timeout time.Duration
	if w.cfg.TaskRunTime != 0 {
		timeout = time.Duration(w.cfg.TaskRunTime) * time.Second
	} else {
		timeout = maxRunTime
	}

	//new a http client with timeout setting
	client := &http.Client{
		Timeout: timeout,
	}

	r, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer r.Body.Close()
	buf, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return "", err
	}
	if r.StatusCode != http.StatusOK {
		return "", errors.NewError(string(buf))
	}

	return string(buf), nil
}
예제 #2
0
func (w *Worker) ExecBin(binPath string, args []string, maxRunTime int64) (string, error) {
	var cmd *exec.Cmd
	var stdout bytes.Buffer
	var stderr bytes.Buffer
	var err error

	if len(args) == 0 {
		cmd = exec.Command(binPath)
	} else {
		cmd = exec.Command(binPath, args...)
	}

	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	cmd.Start() // attention!

	err, _ = w.CmdRunWithTimeout(cmd,
		time.Duration(maxRunTime)*time.Second,
	)
	if err != nil {
		return "", err
	}
	if len(stderr.String()) != 0 {
		errMsg := strings.TrimRight(stderr.String(), "\n")
		return "", errors.NewError(errMsg)
	}

	return strings.TrimRight(stdout.String(), "\n"), nil
}
예제 #3
0
파일: task.go 프로젝트: NotBadPad/kingtask
func (k *BrokerClient) Delay(t *TaskRequest) error {
	if t == nil {
		return nil
	}
	reply := make([]byte, 512)
	result := new(StatusResult)

	ret, err := json.Marshal(t)
	if err != nil {
		fmt.Printf("mashal error:%s\n", err.Error())
		return err
	}
	sendBuf := make([]byte, len(ret)+1)
	sendBuf[0] = config.TypeRequestTask
	copy(sendBuf[1:], ret)
	_, err = k.BrokerConn.Write(sendBuf)
	if err != nil {
		fmt.Printf("Write error:%s\n", err.Error())
		return err
	}
	readLen, err := k.BrokerConn.Read(reply)
	if err != nil {
		fmt.Printf("Read error:%s\n", err.Error())
		return err
	}
	err = json.Unmarshal(reply[:readLen], result)
	if err != nil {
		fmt.Printf("Unmarshal error:%s\n", err.Error())
		return err
	}
	if result.Status == 1 {
		return errors.NewError(result.Message)
	}

	return nil
}