Skip to content

Commit

Permalink
fix(lmeval): Send Job logs to Pod StdOut (#358)
Browse files Browse the repository at this point in the history
* Send job logs to pod logs

* Preserve pipewriter with newline introduction

* Fix error return of the pod log writer

* Update driver.go

---------

Co-authored-by: Rui Vieira <[email protected]>
  • Loading branch information
RobGeada and ruivieira authored Nov 12, 2024
1 parent 4795c53 commit 2a8f7d7
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions controllers/lmes/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func (d *driverImpl) Run() error {
}
}
toConsole(filepath.Join(d.Option.OutputPath, "stdout.log"))
toConsole(filepath.Join(d.Option.OutputPath, "stderr.log"))
// TODO: Remove so that log is not printed again at job's completion
// toConsole(filepath.Join(d.Option.OutputPath, "stderr.log"))

d.updateCompleteStatus(execErr)

Expand Down Expand Up @@ -283,6 +284,29 @@ func (dc *driverComm) notifyShutdownWait() {
dc.connection <- 1
}

type PodLogAndPipeWriter struct {
pw *io.PipeWriter
}

func (f PodLogAndPipeWriter) Write(p []byte) (n int, err error) {
// format lm-eval output to stdout and the pipewriter
line := string(p)
res, _ := fmt.Print(line)
if _, err := f.pw.Write(p); err != nil {
return 0, err
}

// carriage returns do not correctly display, so replace with newlines
// carriage returns also break the pipewriter, so make sure we get newlines as well.
if strings.ContainsAny(line, "\r") && !strings.ContainsAny(line, "\n") {
fmt.Print("\n")
if _, err := f.pw.Write([]byte("\n")); err != nil {
return 0, err
}
}
return res, nil
}

func (d *driverImpl) exec() error {
// create Unitxt task recipes
if err := d.createTaskRecipes(); err != nil {
Expand Down Expand Up @@ -317,7 +341,7 @@ func (d *driverImpl) exec() error {
// have a pipe to check the output and report progress
// lm-eval's outputs are in the stderr
pr, pw := io.Pipe()
mwriter := io.MultiWriter(stderr, pw)
mwriter := io.MultiWriter(stderr, PodLogAndPipeWriter{pw})
scanner := bufio.NewScanner(pr)

executor := exec.Command(d.Option.Args[0], args...)
Expand Down

0 comments on commit 2a8f7d7

Please sign in to comment.