From 37b6255e4255d57f7d853fdb3813163bdeed472d Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 8 Nov 2015 17:43:48 -0800 Subject: [PATCH] Move process to go-process --- cmd/barak/barak.go | 7 ++-- cmd/barak/main.go | 8 ++-- cmd/barak/types/responses.go | 2 +- process/process.go | 75 ------------------------------------ 4 files changed, 8 insertions(+), 84 deletions(-) delete mode 100644 process/process.go diff --git a/cmd/barak/barak.go b/cmd/barak/barak.go index 2105b5068..187daa9fd 100644 --- a/cmd/barak/barak.go +++ b/cmd/barak/barak.go @@ -1,4 +1,3 @@ - package main import ( @@ -11,10 +10,10 @@ import ( "sync" "time" + . "github.com/tendermint/go-common" + pcm "github.com/tendermint/go-process" "github.com/tendermint/go-wire" . "github.com/tendermint/tendermint/cmd/barak/types" - . "github.com/tendermint/go-common" - pcm "github.com/tendermint/tendermint/process" "github.com/tendermint/tendermint/rpc/server" ) @@ -137,7 +136,7 @@ func (brk *Barak) StopProcess(label string, kill bool) error { return fmt.Errorf("Process does not exist: %v", label) } - err := pcm.Stop(proc, kill) + err := proc.StopProcess(kill) return err } diff --git a/cmd/barak/main.go b/cmd/barak/main.go index 0f2445478..e587871be 100644 --- a/cmd/barak/main.go +++ b/cmd/barak/main.go @@ -16,12 +16,12 @@ import ( "reflect" "time" - . "github.com/tendermint/tendermint/cmd/barak/types" . "github.com/tendermint/go-common" cfg "github.com/tendermint/go-config" - pcm "github.com/tendermint/tendermint/process" - "github.com/tendermint/tendermint/rpc/server" + pcm "github.com/tendermint/go-process" "github.com/tendermint/go-wire" + . "github.com/tendermint/tendermint/cmd/barak/types" + "github.com/tendermint/tendermint/rpc/server" ) const BarakVersion = "0.0.1" @@ -162,7 +162,7 @@ func StartProcess(wait bool, label string, execPath string, args []string, input if err != nil { return nil, err } - proc, err := pcm.Create(label, execPath, args, inFile, outFile) + proc, err := pcm.StartProcess(label, execPath, args, inFile, outFile) if err != nil { return nil, err } diff --git a/cmd/barak/types/responses.go b/cmd/barak/types/responses.go index 40ea1eb25..dab26f75d 100644 --- a/cmd/barak/types/responses.go +++ b/cmd/barak/types/responses.go @@ -1,7 +1,7 @@ package types import ( - pcm "github.com/tendermint/tendermint/process" + pcm "github.com/tendermint/go-process" ) type ResponseStatus struct { diff --git a/process/process.go b/process/process.go deleted file mode 100644 index 5c27f14eb..000000000 --- a/process/process.go +++ /dev/null @@ -1,75 +0,0 @@ -package process - -import ( - "fmt" - "io" - "os" - "os/exec" - "time" -) - -type Process struct { - Label string - ExecPath string - Args []string - Pid int - StartTime time.Time - EndTime time.Time - Cmd *exec.Cmd `json:"-"` - ExitState *os.ProcessState `json:"-"` - InputFile io.Reader `json:"-"` - OutputFile io.WriteCloser `json:"-"` - WaitCh chan struct{} `json:"-"` -} - -// execPath: command name -// args: args to command. (should not include name) -func Create(label string, execPath string, args []string, inFile io.Reader, outFile io.WriteCloser) (*Process, error) { - cmd := exec.Command(execPath, args...) - cmd.Stdout = outFile - cmd.Stderr = outFile - cmd.Stdin = inFile - if err := cmd.Start(); err != nil { - return nil, err - } - proc := &Process{ - Label: label, - ExecPath: execPath, - Args: args, - Pid: cmd.Process.Pid, - StartTime: time.Now(), - Cmd: cmd, - ExitState: nil, - InputFile: inFile, - OutputFile: outFile, - WaitCh: make(chan struct{}), - } - go func() { - err := proc.Cmd.Wait() - if err != nil { - fmt.Printf("Process exit: %v\n", err) - if exitError, ok := err.(*exec.ExitError); ok { - proc.ExitState = exitError.ProcessState - } - } - proc.ExitState = proc.Cmd.ProcessState - proc.EndTime = time.Now() // TODO make this goroutine-safe - err = proc.OutputFile.Close() - if err != nil { - fmt.Printf("Error closing output file for %v: %v\n", proc.Label, err) - } - close(proc.WaitCh) - }() - return proc, nil -} - -func Stop(proc *Process, kill bool) error { - defer proc.OutputFile.Close() - if kill { - fmt.Printf("Killing process %v\n", proc.Cmd.Process) - return proc.Cmd.Process.Kill() - } else { - fmt.Printf("Stopping process %v\n", proc.Cmd.Process) - return proc.Cmd.Process.Signal(os.Interrupt) - } -}