Skip to content

Commit

Permalink
wait after container exit in logs cmd instead of json_logger for unpr…
Browse files Browse the repository at this point in the history
…ocessed data

Signed-off-by: Mrudul Harwani <mharwani@amazon.com>
  • Loading branch information
mharwani committed Jun 29, 2023
1 parent ed7eec1 commit 97ef2b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
3 changes: 3 additions & 0 deletions pkg/cmd/container/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/containerd/containerd"
"github.com/containerd/containerd/errdefs"
Expand Down Expand Up @@ -90,6 +91,8 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
// Setup goroutine to send stop event if container task finishes:
go func() {
<-waitCh
// wait 100ms to let logViewer process data sent after container exit, if any
time.Sleep(100 * time.Millisecond)
logrus.Debugf("container task has finished, sending kill signal to log viewer")
stopChannel <- os.Interrupt
}()
Expand Down
63 changes: 26 additions & 37 deletions pkg/logging/json_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,46 +157,37 @@ func viewLogsJSONFileDirect(lvopts LogViewOptions, jsonLogFilePath string, stdou
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at position %d: %s", jsonLogFilePath, lastPos, err)
}
fin.Close()

readFromLastPos := func() error {
// Re-open the file and seek to the last-consumed offset.
fin, err = os.OpenFile(jsonLogFilePath, os.O_RDONLY, 0400)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while trying to re-open JSON logfile %q: %s", jsonLogFilePath, err)
}
_, err = fin.Seek(lastPos, 0)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at position %d: %s", jsonLogFilePath, lastPos, err)
}

err = jsonfile.Decode(stdout, stderr, fin, lvopts.Timestamps, lvopts.Since, lvopts.Until, 0)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while doing follow-up decoding of JSON logfile %q at starting position %d: %s", jsonLogFilePath, lastPos, err)
}

// Record current file seek position before looping again.
lastPos, err = fin.Seek(0, io.SeekCurrent)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at current position: %s", jsonLogFilePath, err)
}
fin.Close()
return nil
}

for {
select {
case <-stopChannel:
logrus.Debugf("received stop signal, re-reading JSON logfile and returning")
// read final logs before returning
return readFromLastPos()
logrus.Debugf("received stop signal while re-reading JSON logfile, returning")
return nil
default:
if err = readFromLastPos(); err != nil {
return err
// Re-open the file and seek to the last-consumed offset.
fin, err = os.OpenFile(jsonLogFilePath, os.O_RDONLY, 0400)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while trying to re-open JSON logfile %q: %s", jsonLogFilePath, err)
}
_, err = fin.Seek(lastPos, 0)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at position %d: %s", jsonLogFilePath, lastPos, err)
}

err = jsonfile.Decode(stdout, stderr, fin, lvopts.Timestamps, lvopts.Since, lvopts.Until, 0)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while doing follow-up decoding of JSON logfile %q at starting position %d: %s", jsonLogFilePath, lastPos, err)
}

// Record current file seek position before looping again.
lastPos, err = fin.Seek(0, io.SeekCurrent)
if err != nil {
fin.Close()
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at current position: %s", jsonLogFilePath, err)
}
fin.Close()
}
// Give the OS a second to breathe before re-opening the file:
time.Sleep(time.Second)
Expand Down Expand Up @@ -233,8 +224,6 @@ func viewLogsJSONFileThroughTailExec(lvopts LogViewOptions, jsonLogFilePath stri
// Setup killing goroutine:
go func() {
<-stopChannel
// sleep 100ms to get logs post container exit
time.Sleep(100 * time.Millisecond)
logrus.Debugf("killing tail logs process with PID: %d", cmd.Process.Pid)
cmd.Process.Kill()
}()
Expand Down

0 comments on commit 97ef2b7

Please sign in to comment.