Blame SOURCES/0001-fix-read-worker-output-using-io.Read.patch

13c0f8
From d75dc60df73a88b0a14c799f3b6f1e8f66cee3d4 Mon Sep 17 00:00:00 2001
13c0f8
From: Link Dupont <link@sub-pop.net>
13c0f8
Date: Tue, 22 Nov 2022 13:07:41 -0500
13c0f8
Subject: [PATCH] fix: read worker output using io.Read
13c0f8
13c0f8
Some workers output a lot of text to stderr and stdout. Rather than
13c0f8
scanning stderr and stdout into a buffer using a bufio.Scanner, read a
13c0f8
fixed number of bytes at a time. This will break lines of output from
13c0f8
the worker in the middle of words, making reading stdout in the logs
13c0f8
more difficult, but avoids the overly verbose workers from hitting the
13c0f8
bufio.ErrTooLong error.
13c0f8
13c0f8
Signed-off-by: Link Dupont <link@sub-pop.net>
13c0f8
---
13c0f8
 cmd/yggd/exec.go | 46 +++++++++++++++++++++++++++++++++-------------
13c0f8
 1 file changed, 33 insertions(+), 13 deletions(-)
13c0f8
13c0f8
diff --git a/cmd/yggd/exec.go b/cmd/yggd/exec.go
13c0f8
index 4eb1757..a2a3d29 100644
13c0f8
--- a/cmd/yggd/exec.go
13c0f8
+++ b/cmd/yggd/exec.go
13c0f8
@@ -1,8 +1,8 @@
13c0f8
 package main
13c0f8
 
13c0f8
 import (
13c0f8
-	"bufio"
13c0f8
 	"fmt"
13c0f8
+	"io"
13c0f8
 	"io/ioutil"
13c0f8
 	"os"
13c0f8
 	"os/exec"
13c0f8
@@ -54,22 +54,42 @@ func startProcess(file string, env []string, delay time.Duration, died chan int)
13c0f8
 	log.Debugf("started process: %v", cmd.Process.Pid)
13c0f8
 
13c0f8
 	go func() {
13c0f8
-		scanner := bufio.NewScanner(stdout)
13c0f8
-		for scanner.Scan() {
13c0f8
-			log.Tracef("[%v] %v", file, scanner.Text())
13c0f8
-		}
13c0f8
-		if err := scanner.Err(); err != nil {
13c0f8
-			log.Errorf("cannot read from stdout: %v", err)
13c0f8
+		for {
13c0f8
+			buf := make([]byte, 4096)
13c0f8
+			n, err := stdout.Read(buf)
13c0f8
+			if n > 0 {
13c0f8
+				log.Tracef("[%v] %v", file, strings.TrimRight(string(buf), "\n\x00"))
13c0f8
+			}
13c0f8
+			if err != nil {
13c0f8
+				switch err {
13c0f8
+				case io.EOF:
13c0f8
+					log.Debugf("%v stdout reached EOF: %v", file, err)
13c0f8
+					return
13c0f8
+				default:
13c0f8
+					log.Errorf("cannot read from stdout: %v", err)
13c0f8
+					continue
13c0f8
+				}
13c0f8
+			}
13c0f8
 		}
13c0f8
 	}()
13c0f8
 
13c0f8
 	go func() {
13c0f8
-		scanner := bufio.NewScanner(stderr)
13c0f8
-		for scanner.Scan() {
13c0f8
-			log.Errorf("[%v] %v", file, scanner.Text())
13c0f8
-		}
13c0f8
-		if err := scanner.Err(); err != nil {
13c0f8
-			log.Errorf("cannot read from stderr: %v", err)
13c0f8
+		for {
13c0f8
+			buf := make([]byte, 4096)
13c0f8
+			n, err := stderr.Read(buf)
13c0f8
+			if n > 0 {
13c0f8
+				log.Errorf("[%v] %v", file, strings.TrimRight(string(buf), "\n\x00"))
13c0f8
+			}
13c0f8
+			if err != nil {
13c0f8
+				switch err {
13c0f8
+				case io.EOF:
13c0f8
+					log.Debugf("%v stderr reached EOF: %v", file, err)
13c0f8
+					return
13c0f8
+				default:
13c0f8
+					log.Errorf("cannot read from stderr: %v", err)
13c0f8
+					continue
13c0f8
+				}
13c0f8
+			}
13c0f8
 		}
13c0f8
 	}()
13c0f8
 
13c0f8
-- 
13c0f8
2.38.1
13c0f8