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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user