From 276d032f175ac5d7a7108e7157ca435e0a82b8c6 Mon Sep 17 00:00:00 2001 From: Qiu Yu Date: Tue, 24 May 2016 00:35:51 -0700 Subject: [PATCH] Also capture stderr output from command --- job.go | 17 ++++++++++++++--- job_test.go | 1 + worker.go | 2 ++ worker_test.go | 5 +++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/job.go b/job.go index 3de210e..a6aad60 100644 --- a/job.go +++ b/job.go @@ -11,6 +11,7 @@ type Job interface { Execute() error GetData() []byte GetResult() []byte + GetErr() []byte } // Job contains boths command and data to apply command on @@ -18,6 +19,7 @@ type ExecJob struct { args []string data []byte result []byte + err []byte } func (j *ExecJob) execute() error { @@ -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) @@ -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 } @@ -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 +} diff --git a/job_test.go b/job_test.go index 889b63f..b0c6d94 100644 --- a/job_test.go +++ b/job_test.go @@ -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 { diff --git a/worker.go b/worker.go index a0e544c..f147496 100644 --- a/worker.go +++ b/worker.go @@ -2,6 +2,7 @@ package fastexec import ( "fmt" + "os" "sync" "time" @@ -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() } } diff --git a/worker_test.go b/worker_test.go index ffb93b9..be4cc8e 100644 --- a/worker_test.go +++ b/worker_test.go @@ -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