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.
This commit is contained in:
Jose Diaz-Gonzalez
2021-01-02 06:13:48 -05:00
parent 5e4c918d40
commit 38d172c477

View File

@@ -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