From 38d172c477d0587516027eed56578f8ea5d1d08a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 2 Jan 2021 06:13:48 -0500 Subject: [PATCH] refactor: add support for debug logging plugin trigger stderr and stdout Previously, this could get a bit hazy when running the command within a goroutine. The new method creates pipes for writing stdout/stderr for each call to PluginTriggerOutput, then debug logs if if trace mode is on. While it is a bit weird to read, it is more correct and should get rid of any parallel processing issues. --- plugins/common/subprocess.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/plugins/common/subprocess.go b/plugins/common/subprocess.go index 8257a8e51..f60362ff1 100644 --- a/plugins/common/subprocess.go +++ b/plugins/common/subprocess.go @@ -80,21 +80,34 @@ func PlugnTrigger(triggerName string, args ...string) error { // PlugnTriggerOutput fire the given plugn trigger with the given args func PlugnTriggerOutput(triggerName string, args ...string) ([]byte, error) { - rescueStderr := os.Stderr - r, w, _ := os.Pipe() - os.Stderr = w - out, err := PlugnTriggerSetup(triggerName, args...).Output() - w.Close() + LogDebug(fmt.Sprintf("plugn trigger %s %v", triggerName, args)) + rE, wE, _ := os.Pipe() + rO, wO, _ := os.Pipe() + session := PlugnTriggerSetup(triggerName, args...) + session.Stderr = wE + session.Stdout = wO + err := session.Run() + wE.Close() + wO.Close() - readStderr, _ := ioutil.ReadAll(r) - os.Stderr = rescueStderr + readStderr, _ := ioutil.ReadAll(rE) + readStdout, _ := ioutil.ReadAll(rO) - var stderr error + stderr := string(readStderr[:]) if err != nil { - stderr = fmt.Errorf(string(readStderr[:])) + err = fmt.Errorf(stderr) } - return out, stderr + if os.Getenv("DOKKU_TRACE") == "1" { + for _, line := range strings.Split(stderr, "\n") { + LogDebug(fmt.Sprintf("plugn trigger %s stderr: %s", triggerName, line)) + } + for _, line := range strings.Split(string(readStdout[:]), "\n") { + LogDebug(fmt.Sprintf("plugn trigger %s stdout: %s", triggerName, line)) + } + } + + return readStdout, err } // PlugnTriggerSetup sets up a plugn trigger call