Skip to content

Commit

Permalink
Add debug log for work unit payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
matoval committed Sep 18, 2024
1 parent b9bab8f commit 543f524
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
32 changes: 30 additions & 2 deletions pkg/controlsvc/controlsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package controlsvc

import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
Expand Down Expand Up @@ -122,8 +123,35 @@ func (s *SockControl) ReadFromConn(message string, out io.Writer, io Copier) err
if err := s.WriteMessage(message); err != nil {
return err
}
if _, err := io.Copy(out, s.conn); err != nil {
return err
isPayloadDebug := os.Getenv("RECEPTOR_PAYLOAD_DEBUG")
if isPayloadDebug != "" {
var data string
reader := bufio.NewReader(s.conn)

for {
var connectType string
if s.conn.LocalAddr().Network() == "unix" {
connectType = "unix socket"
} else {
connectType = "network connection"
}
response, err := reader.ReadString('\n')
if err != nil {
if err.Error() != "EOF" {
MainInstance.nc.GetLogger().Error("Error reading from %v: %v \n", connectType, err)
}
break

Check failure on line 143 in pkg/controlsvc/controlsvc.go

View workflow job for this annotation

GitHub Actions / lint-receptor

break with no blank line before (nlreturn)
}
data += response
MainInstance.nc.GetLogger().Debug("Response from %v: %v", connectType, response)
}
if _, err := out.Write([]byte(data)); err != nil {
return err
}
} else {
if _, err := io.Copy(out, s.conn); err != nil {
return err
}
}

return nil
Expand Down
28 changes: 27 additions & 1 deletion pkg/workceptor/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package workceptor

import (
"bufio"
"context"
"flag"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -112,7 +114,31 @@ func commandRunner(command string, params string, unitdir string) error {
if err != nil {
return err
}
cmd.Stdin = stdin
isPayloadDebug := os.Getenv("RECEPTOR_PAYLOAD_DEBUG")
if isPayloadDebug != "" {
var data string
splitUnitDir := strings.Split(unitdir, "/")
workUnitID := splitUnitDir[len(splitUnitDir) - 1]
reader := bufio.NewReader(stdin)
stdinStream, err := cmd.StdinPipe()
if err != nil {
return err
}
for {
response, err := reader.ReadString('\n')
if err != nil {
if err.Error() != "EOF" {
MainInstance.nc.GetLogger().Error("Error reading work unit %v stdin: %v\n", workUnitID, err)
}
break

Check failure on line 133 in pkg/workceptor/command.go

View workflow job for this annotation

GitHub Actions / lint-receptor

break with no blank line before (nlreturn)
}
data += response
MainInstance.nc.GetLogger().Debug("Work unit %v stdin: %v", workUnitID, response)
}
io.WriteString(stdinStream, data)
} else {
cmd.Stdin = stdin
}
stdout, err := os.OpenFile(path.Join(unitdir, "stdout"), os.O_CREATE+os.O_WRONLY+os.O_SYNC, 0o600)
if err != nil {
return err
Expand Down

0 comments on commit 543f524

Please sign in to comment.