Skip to content

Commit

Permalink
Also capture stderr output from command
Browse files Browse the repository at this point in the history
  • Loading branch information
unicell committed May 24, 2016
1 parent 9bde85d commit 276d032
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
17 changes: 14 additions & 3 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ type Job interface {
Execute() error
GetData() []byte
GetResult() []byte
GetErr() []byte
}

// Job contains boths command and data to apply command on
type ExecJob struct {
args []string
data []byte
result []byte
err []byte
}

func (j *ExecJob) execute() error {
Expand All @@ -27,6 +29,7 @@ func (j *ExecJob) execute() error {
cmd := exec.Command(j.args[0], j.args[1:]...)
cmdIn, _ := cmd.StdinPipe()
cmdOut, _ := cmd.StdoutPipe()
cmdErr, _ := cmd.StderrPipe()

cmd.Start()
_, err := cmdIn.Write(j.data)
Expand All @@ -36,15 +39,18 @@ func (j *ExecJob) execute() error {
}
cmdIn.Close()

cmdBytes, err := ioutil.ReadAll(cmdOut)
cmdOutBytes, err := ioutil.ReadAll(cmdOut)
cmdErrBytes, err := ioutil.ReadAll(cmdErr)
cmd.Wait()
if err != nil {
glog.Errorf("Error reading from pipe %v", err)
return err
}

j.result = cmdBytes
glog.V(4).Infof("--> executor - result\n>>\n%s<<\n", string(cmdBytes))
j.result = cmdOutBytes
j.err = cmdErrBytes
glog.V(4).Infof("--> executor - result\n>>\n%s<<\n", string(cmdOutBytes))
glog.V(4).Infof("--> executor - result - err\n>>\n%s<<\n", string(cmdErrBytes))

return nil
}
Expand All @@ -64,3 +70,8 @@ func (j ExecJob) GetData() []byte {
func (j ExecJob) GetResult() []byte {
return j.result
}

// Get err result from a Job
func (j ExecJob) GetErr() []byte {
return j.err
}
1 change: 1 addition & 0 deletions job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestExecJobs(t *testing.T) {
err: &os.PathError{"write", "|1", errors.New("bad file descriptor")},
expected: []byte(""),
},
// TODO(qiuyu): stderr test
}

for _, c := range testCases {
Expand Down
2 changes: 2 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fastexec

import (
"fmt"
"os"
"sync"
"time"

Expand Down Expand Up @@ -37,6 +38,7 @@ func StateMonitor(in <-chan Job) {
glog.V(2).Infof("--> state monitor")
for j := range in {
fmt.Print(string(j.GetResult()))
fmt.Fprintf(os.Stderr, string(j.GetErr()))
outWg.Done()
}
}
Expand Down
5 changes: 5 additions & 0 deletions worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (j FakeJob) GetResult() []byte {
return j.data
}

// Get err result from a Job
func (j FakeJob) GetErr() []byte {
return j.data
}

func TestWorker(t *testing.T) {
testCases := []struct {
about string
Expand Down

0 comments on commit 276d032

Please sign in to comment.