diff options
author | Andrew Newdigate <andrew@troupe.co> | 2017-04-25 20:50:23 +0300 |
---|---|---|
committer | Andrew Newdigate <andrew@troupe.co> | 2017-04-25 20:50:23 +0300 |
commit | 624c422de65b682f7ecbd6c62b05b38ac89d368b (patch) | |
tree | 16c6b40e817cfeca58fb502530438dc13f31ad87 | |
parent | 19aabc1a95f27a76e5d6e80dc989f548471b516d (diff) |
Shutdown goroutine when process is done
-rw-r--r-- | internal/helper/command_wrapper_go1.6.go | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/internal/helper/command_wrapper_go1.6.go b/internal/helper/command_wrapper_go1.6.go index e7d83e4d1..9643c9a9e 100644 --- a/internal/helper/command_wrapper_go1.6.go +++ b/internal/helper/command_wrapper_go1.6.go @@ -14,10 +14,29 @@ func CommandWrapper(ctx context.Context, name string, arg ...string) *exec.Cmd { command := exec.Command(name, arg...) if ctx != nil { + // Create a channel to listen to the command completion + done := make(chan error, 1) go func() { - <-ctx.Done() - log.Printf("Context done, killing process") - command.Kill() + done <- cmd.Wait() + }() + + // Wait for the process to shutdown or the + // context to be complete + go func() { + select { + case <-ctx.Done(): + log.Printf("Context done, killing process") + command.Kill() + + case err <- done: + if err != nil { + log.Printf("process done with error = %v", err) + } else { + log.Print("process done gracefully without error") + } + + } + }() } |