From e8aaacff5e5b327e9f194c3c97e5382b673e55c7 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 2 Jan 2021 06:11:34 -0500 Subject: [PATCH 1/3] refactor: simplify code --- plugins/common/common.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/common/common.go b/plugins/common/common.go index 6d9c5ae5a..78e938f84 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -74,13 +74,10 @@ func GetAppScheduler(appName string) string { wg.Add(1) go func() { defer wg.Done() - b, _ := PlugnTriggerOutput("config-get", []string{appName, "DOKKU_SCHEDULER"}...) - value := strings.TrimSpace(string(b[:])) - if value != "" { - appScheduler = value - } + appScheduler = getAppScheduler(appName) }() } + wg.Add(1) go func() { defer wg.Done() @@ -95,6 +92,15 @@ func GetAppScheduler(appName string) string { return appScheduler } +func getAppScheduler(appName string) string { + b, _ := PlugnTriggerOutput("config-get", []string{appName, "DOKKU_SCHEDULER"}...) + value := strings.TrimSpace(string(b[:])) + if value != "" { + return value + } + return "" +} + // GetGlobalScheduler fetchs the global scheduler func GetGlobalScheduler() string { b, _ := PlugnTriggerOutput("config-get-global", []string{"DOKKU_SCHEDULER"}...) From 5e4c918d403caca5368ab94410acc0a9f931deb2 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 2 Jan 2021 06:11:51 -0500 Subject: [PATCH 2/3] feat: trim any extra debug log prefixes --- plugins/common/log.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/common/log.go b/plugins/common/log.go index fe1193e1e..e939cd1b2 100644 --- a/plugins/common/log.go +++ b/plugins/common/log.go @@ -110,7 +110,7 @@ func LogStderr(text string) { // LogDebug is the debug log formatter func LogDebug(text string) { - if os.Getenv("DOKKU_TRACE") == "" { - fmt.Fprintln(os.Stderr, fmt.Sprintf(" ? %s", text)) + if os.Getenv("DOKKU_TRACE") == "1" { + fmt.Fprintln(os.Stderr, fmt.Sprintf(" ? %s", strings.TrimPrefix(text, " ? "))) } } From 38d172c477d0587516027eed56578f8ea5d1d08a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 2 Jan 2021 06:13:48 -0500 Subject: [PATCH 3/3] 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