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