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

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