From 78dd4f2ecb9f85fda87e581b9b2800618223154f Mon Sep 17 00:00:00 2001 From: jgallucci32 Date: Sat, 20 Jun 2020 09:45:18 -0700 Subject: Stop following logs using timers This incorporates code from PR #6591 and #6614 but does not use event channels to detect container state and rather uses timers with a defined wait duration before calling t.StopAtEOF() to ensure the last log entry is output before a container exits. The polling interval is set to 250 milliseconds based on polling interval defined in hpcloud/tail here: https://github.com/hpcloud/tail/blob/v1.0.0/watch/polling.go#L117 Co-authored-by: Qi Wang Signed-off-by: jgallucci32 --- libpod/container_log.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'libpod') diff --git a/libpod/container_log.go b/libpod/container_log.go index 071882bc2..9a2f8aa2f 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -1,7 +1,9 @@ package libpod import ( + "fmt" "os" + "time" "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/logs" @@ -81,5 +83,33 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l } options.WaitGroup.Done() }() + // Check if container is still running or paused + if options.Follow { + go func() { + for { + state, err := c.State() + if err != nil { + time.Sleep(250 * time.Millisecond) + tailError := t.StopAtEOF() + if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" { + logrus.Error(tailError) + } + if errors.Cause(err) != define.ErrNoSuchCtr { + logrus.Error(err) + } + break + } + if state != define.ContainerStateRunning && state != define.ContainerStatePaused { + time.Sleep(250 * time.Millisecond) + tailError := t.StopAtEOF() + if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" { + logrus.Error(tailError) + } + break + } + time.Sleep(250 * time.Millisecond) + } + }() + } return nil } -- cgit v1.2.3-54-g00ecf From 8926e1f03bc82d75a295a0784c2756ea37c9b031 Mon Sep 17 00:00:00 2001 From: jgallucci32 Date: Sun, 21 Jun 2020 09:31:22 -0700 Subject: Use POLL_DURATION for timer Signed-off-by: jgallucci32 --- libpod/container_log.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'libpod') diff --git a/libpod/container_log.go b/libpod/container_log.go index 9a2f8aa2f..67380397a 100644 --- a/libpod/container_log.go +++ b/libpod/container_log.go @@ -7,6 +7,7 @@ import ( "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/logs" + "github.com/hpcloud/tail/watch" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -88,8 +89,8 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l go func() { for { state, err := c.State() + time.Sleep(watch.POLL_DURATION) if err != nil { - time.Sleep(250 * time.Millisecond) tailError := t.StopAtEOF() if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" { logrus.Error(tailError) @@ -100,14 +101,12 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l break } if state != define.ContainerStateRunning && state != define.ContainerStatePaused { - time.Sleep(250 * time.Millisecond) tailError := t.StopAtEOF() if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" { logrus.Error(tailError) } break } - time.Sleep(250 * time.Millisecond) } }() } -- cgit v1.2.3-54-g00ecf