From 5102f7a9cbc0f0f336c49834a5ef4ca9afcfbf27 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sat, 17 Oct 2015 15:12:07 -0700 Subject: [PATCH] refactor process.Process to take files --- cmd/barak/main.go | 22 ++++++++++++++++++---- cmd/debora/main.go | 3 +-- process/process.go | 45 +++++++-------------------------------------- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/cmd/barak/main.go b/cmd/barak/main.go index 2c5e5085..ca64e417 100644 --- a/cmd/barak/main.go +++ b/cmd/barak/main.go @@ -5,21 +5,23 @@ package main // TODO: Nonrepudiable command log import ( + "bytes" "errors" "flag" "fmt" "io" + "io/ioutil" "net/http" "os" "reflect" "time" - "github.com/tendermint/tendermint/wire" . "github.com/tendermint/tendermint/cmd/barak/types" . "github.com/tendermint/tendermint/common" cfg "github.com/tendermint/tendermint/config" pcm "github.com/tendermint/tendermint/process" "github.com/tendermint/tendermint/rpc/server" + "github.com/tendermint/tendermint/wire" ) const BarakVersion = "0.0.1" @@ -152,8 +154,13 @@ func StartProcess(wait bool, label string, execPath string, args []string, input if err != nil { return nil, fmt.Errorf("Failed to create outputs dir: %v", err) } + inFile := bytes.NewReader([]byte(input)) outPath := Fmt("%v/outputs/%v_%v.out", barak_.RootDir(), label, time.Now().Format("2006_01_02_15_04_05_MST")) - proc, err := pcm.Create(pcm.ProcessModeDaemon, label, execPath, args, input, outPath) + outFile, err := OpenAutoFile(outPath) + if err != nil { + return nil, err + } + proc, err := pcm.Create(label, execPath, args, inFile, outFile) if err != nil { return nil, err } @@ -161,7 +168,14 @@ func StartProcess(wait bool, label string, execPath string, args []string, input if wait { <-proc.WaitCh - output := pcm.ReadOutput(proc) + + // read output from outPath + outputBytes, err := ioutil.ReadFile(outPath) + if err != nil { + fmt.Sprintf("ERROR READING OUTPUT: %v", err) + } + output := string(outputBytes) + // fmt.Println("Read output", output) if proc.ExitState == nil { return &ResponseStartProcess{ @@ -260,7 +274,7 @@ func ServeFileHandler(w http.ResponseWriter, req *http.Request) { http.Error(w, Fmt("Unknown process label: %v", path), 400) return } - path = proc.OutputPath + path = proc.OutputFile.(*os.File).Name() } file, err := os.Open(path) if err != nil { diff --git a/cmd/debora/main.go b/cmd/debora/main.go index ce498458..45a1b0c5 100644 --- a/cmd/debora/main.go +++ b/cmd/debora/main.go @@ -11,10 +11,10 @@ import ( "sync" acm "github.com/tendermint/tendermint/account" - "github.com/tendermint/tendermint/wire" btypes "github.com/tendermint/tendermint/cmd/barak/types" . "github.com/tendermint/tendermint/common" cfg "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/wire" ) func remoteNick(remote string) string { @@ -268,7 +268,6 @@ func cliListProcesses(c *cli.Context) { endTimeStr := proc.EndTime.String() fmt.Printf(", stopped at %v\n", Yellow(endTimeStr)) } - fmt.Printf(" stdout/stderr goes to %v\n", proc.OutputPath) } } }(remote) diff --git a/process/process.go b/process/process.go index 0d297432..03b0a46c 100644 --- a/process/process.go +++ b/process/process.go @@ -1,15 +1,11 @@ package process import ( - "bytes" "fmt" "io" - "io/ioutil" "os" "os/exec" "time" - - . "github.com/tendermint/tendermint/common" ) type Process struct { @@ -19,39 +15,20 @@ type Process struct { Pid int StartTime time.Time EndTime time.Time - OutputPath string Cmd *exec.Cmd `json:"-"` ExitState *os.ProcessState `json:"-"` - OutputFile *AutoFile `json:"-"` + InputFile io.Reader `json:"-"` + OutputFile io.WriteCloser `json:"-"` WaitCh chan struct{} `json:"-"` } -const ( - ProcessModeStd = iota - ProcessModeDaemon -) - // execPath: command name // args: args to command. (should not include name) -func Create(mode int, label string, execPath string, args []string, input string, outPath string) (*Process, error) { - outFile, err := OpenAutoFile(outPath) - if err != nil { - return nil, err - } +func Create(label string, execPath string, args []string, inFile io.Reader, outFile io.WriteCloser) (*Process, error) { cmd := exec.Command(execPath, args...) - switch mode { - case ProcessModeStd: - cmd.Stdout = io.MultiWriter(os.Stdout, outFile) - cmd.Stderr = io.MultiWriter(os.Stderr, outFile) - cmd.Stdin = nil - case ProcessModeDaemon: - cmd.Stdout = outFile - cmd.Stderr = outFile - cmd.Stdin = nil - } - if input != "" { - cmd.Stdin = bytes.NewReader([]byte(input)) - } + cmd.Stdout = outFile + cmd.Stderr = outFile + cmd.Stdin = inFile if err := cmd.Start(); err != nil { return nil, err } @@ -61,9 +38,9 @@ func Create(mode int, label string, execPath string, args []string, input string Args: args, Pid: cmd.Process.Pid, StartTime: time.Now(), - OutputPath: outPath, Cmd: cmd, ExitState: nil, + InputFile: inFile, OutputFile: outFile, WaitCh: make(chan struct{}), } @@ -85,14 +62,6 @@ func Create(mode int, label string, execPath string, args []string, input string return proc, nil } -func ReadOutput(proc *Process) string { - output, err := ioutil.ReadFile(proc.OutputPath) - if err != nil { - return fmt.Sprintf("ERROR READING OUTPUT: %v", err) - } - return string(output) -} - func Stop(proc *Process, kill bool) error { defer proc.OutputFile.Close() if kill {