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

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