Skip to content

Commit

Permalink
Better handling of concurrent printing
Browse files Browse the repository at this point in the history
  • Loading branch information
jblebrun committed Apr 16, 2024
1 parent 82ec77d commit 294c9b4
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions internal/builders/docker/pkg/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,34 +480,34 @@ func (c *GitClient) checkoutGitCommit() error {

type TempFileResult struct {
File *os.File
Err error
Err error
}

// A helper function used by saveToTempFile to process one individual file.
func saveOneTempFile(verbose bool, reader io.Reader, ch chan TempFileResult) {
var allBytes []byte
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
bytes := scanner.Bytes()
allBytes = append(allBytes, bytes...)
allBytes = append(allBytes, '\n')

if verbose {
fmt.Printf("%s\n", bytes)
}
func saveOneTempFile(verbose bool, reader io.Reader, fileChannel chan TempFileResult, printChannel chan string) {
var allBytes []byte
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
bytes := scanner.Bytes()
allBytes = append(allBytes, bytes...)
allBytes = append(allBytes, '\n')

if verbose {
printChannel <- string(bytes)
}
}

tmpfile, err := os.CreateTemp("", "log-*.txt")
if err != nil {
ch <- TempFileResult { Err: err }
return
}
if _, err := tmpfile.Write(allBytes); err != nil {
tmpfile.Close()
ch <- TempFileResult { Err: fmt.Errorf("couldn't write bytes to tempfile: %v", err) }
}
tmpfile, err := os.CreateTemp("", "log-*.txt")
if err != nil {
fileChannel <- TempFileResult{Err: err}
return
}
if _, err := tmpfile.Write(allBytes); err != nil {
tmpfile.Close()
fileChannel <- TempFileResult{Err: fmt.Errorf("couldn't write bytes to tempfile: %v", err)}
}

ch <- TempFileResult { File: tmpfile }
fileChannel <- TempFileResult{File: tmpfile}
}

// saveToTempFile creates a tempfile in `/tmp` and writes the content of the
Expand All @@ -518,24 +518,30 @@ func saveToTempFile(verbose bool, readers ...io.Reader) ([]string, error) {
fmt.Print("\n\n>>>>>>>>>>>>>> output from command <<<<<<<<<<<<<<\n")
}
var wg sync.WaitGroup
var ch = make(chan TempFileResult, len(readers))
var fileChannel = make(chan TempFileResult, len(readers))
var printChannel = make(chan string)

for _, reader := range readers {
wg.Add(1)
go func(reader io.Reader) {
defer wg.Done()
saveOneTempFile(verbose, reader, ch)
saveOneTempFile(verbose, reader, fileChannel, printChannel)
}(reader)
}

// Close the channel once all goroutines have finished.
go func() {
wg.Wait()
close(ch)
close(printChannel)
close(fileChannel)
}()

for line := range printChannel {
fmt.Println(line)
}

var files []string
for result := range ch {
for result := range fileChannel {
if result.Err != nil {
return nil, result.Err
}
Expand Down

0 comments on commit 294c9b4

Please sign in to comment.