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 committed 2021-01-02 06:13:48 -05:00
1 parent 5e4c918d40
commit 38d172c477
1 file changed
+23 -10
+23 -10
View File
@@ -80,21 +80,34 @@ func PlugnTrigger(triggerName string, args ...string) error {
// PlugnTriggerOutput fire the given plugn trigger with the given args // PlugnTriggerOutput fire the given plugn trigger with the given args
func PlugnTriggerOutput(triggerName string, args ...string) ([]byte, error) { func PlugnTriggerOutput(triggerName string, args ...string) ([]byte, error) {
rescueStderr := os.Stderr LogDebug(fmt.Sprintf("plugn trigger %s %v", triggerName, args))
r, w, _ := os.Pipe() rE, wE, _ := os.Pipe()
os.Stderr = w rO, wO, _ := os.Pipe()
out, err := PlugnTriggerSetup(triggerName, args...).Output() session := PlugnTriggerSetup(triggerName, args...)
w.Close() session.Stderr = wE
session.Stdout = wO
err := session.Run()
wE.Close()
wO.Close()
readStderr, _ := ioutil.ReadAll(r) readStderr, _ := ioutil.ReadAll(rE)
os.Stderr = rescueStderr readStdout, _ := ioutil.ReadAll(rO)
var stderr error stderr := string(readStderr[:])
if err != nil { 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 // PlugnTriggerSetup sets up a plugn trigger call