From 372cfb509dd8804d550359f26615e431e5c87649 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Thu, 16 Apr 2015 10:54:07 -0700 Subject: [PATCH] Fixed --wait --- cmd/barak/main.go | 12 ++++++++++-- cmd/barak/types/responses.go | 1 + process/process.go | 22 ++++++++++------------ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/cmd/barak/main.go b/cmd/barak/main.go index 69942447..5301d895 100644 --- a/cmd/barak/main.go +++ b/cmd/barak/main.go @@ -165,8 +165,16 @@ func RunProcess(wait bool, label string, execPath string, args []string, input s } if wait { - exitErr := pcm.Wait(proc) - return nil, exitErr + <-proc.WaitCh + if proc.ExitState == nil { + return &ResponseRunProcess{ + Success: true, + }, nil + } else { + return &ResponseRunProcess{ + Success: proc.ExitState.Success(), // Would be always false? + }, nil + } } else { return &ResponseRunProcess{}, nil } diff --git a/cmd/barak/types/responses.go b/cmd/barak/types/responses.go index 3f076ed5..cb3b71fe 100644 --- a/cmd/barak/types/responses.go +++ b/cmd/barak/types/responses.go @@ -10,6 +10,7 @@ type ResponseStatus struct { } type ResponseRunProcess struct { + Success bool } type ResponseStopProcess struct { diff --git a/process/process.go b/process/process.go index d4b88a9d..3c8ddcd0 100644 --- a/process/process.go +++ b/process/process.go @@ -29,6 +29,7 @@ type Process struct { Cmd *exec.Cmd `json:"-"` ExitState *os.ProcessState `json:"-"` OutputFile *os.File `json:"-"` + WaitCh chan struct{} `json:"-"` } const ( @@ -56,8 +57,6 @@ func Create(mode int, label string, execPath string, args []string, input string } if err := cmd.Start(); err != nil { return nil, err - } else { - fmt.Printf("Success!") } proc := &Process{ Label: label, @@ -68,23 +67,22 @@ func Create(mode int, label string, execPath string, args []string, input string Cmd: cmd, ExitState: nil, OutputFile: outFile, + WaitCh: make(chan struct{}), } go func() { - Wait(proc) + 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.EndTime = time.Now() // TODO make this goroutine-safe + close(proc.WaitCh) }() return proc, nil } -func Wait(proc *Process) error { - exitErr := proc.Cmd.Wait() - if exitErr != nil { - fmt.Printf("Process exit: %v\n", exitErr) - proc.ExitState = exitErr.(*exec.ExitError).ProcessState - } - return exitErr -} - func Stop(proc *Process, kill bool) error { if kill { return proc.Cmd.Process.Kill()