Skip to content

Commit

Permalink
Merge pull request #34 from theojulienne/bugfix/missing-output
Browse files Browse the repository at this point in the history
Ensure output and monitoring goroutines complete before finalising job.
  • Loading branch information
keithpitt committed Jun 13, 2014
2 parents 842ea6a + 22ef6ec commit 4a095c0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions buildbox/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"errors"
"syscall"
"sync"
)

type Process struct {
Expand Down Expand Up @@ -66,6 +67,8 @@ func RunScript(dir string, script string, env []string, callback func(Process))
process.Running = true

var buffer bytes.Buffer
var w sync.WaitGroup
w.Add(2)

go func() {
// Copy the pty to our buffer. This will block until it EOF's
Expand All @@ -74,6 +77,7 @@ func RunScript(dir string, script string, env []string, callback func(Process))
if err != nil {
log.Printf("io.Copy failed with error: %s\n", err)
}
w.Done()
}()

go func(){
Expand All @@ -87,6 +91,7 @@ func RunScript(dir string, script string, env []string, callback func(Process))
// Sleep for 1 second
time.Sleep(1000 * time.Millisecond)
}
w.Done()
}()

// Wait until the process has finished
Expand All @@ -96,6 +101,9 @@ func RunScript(dir string, script string, env []string, callback func(Process))
// of the script
process.Running = false
process.ExitStatus = getExitStatus(waitResult)

// wait for the Copy and incremental output to finish first
w.Wait()
process.Output = buffer.String()

// No error occured so we can return nil
Expand Down

0 comments on commit 4a095c0

Please sign in to comment.