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

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