From 9016387bba45bd681d64e6b01f9c94f7bbb0448a Mon Sep 17 00:00:00 2001 From: Ashley Cui Date: Wed, 17 Feb 2021 15:02:22 -0500 Subject: Fix journald logs --follow Previously, --follow with a podman logs using journald would not exit Signed-off-by: Ashley Cui --- libpod/container_log_linux.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'libpod') diff --git a/libpod/container_log_linux.go b/libpod/container_log_linux.go index af46191d0..713db9c1e 100644 --- a/libpod/container_log_linux.go +++ b/libpod/container_log_linux.go @@ -11,8 +11,10 @@ import ( "strings" "time" + "github.com/containers/podman/v2/libpod/define" "github.com/containers/podman/v2/libpod/logs" journal "github.com/coreos/go-systemd/v22/sdjournal" + "github.com/hpcloud/tail/watch" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -65,8 +67,12 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption if options.Tail == math.MaxInt64 { r.Rewind() } + state, err := c.State() + if err != nil { + return err + } - if options.Follow { + if options.Follow && state == define.ContainerStateRunning { go func() { done := make(chan bool) until := make(chan time.Time) @@ -78,6 +84,21 @@ func (c *Container) readFromJournal(ctx context.Context, options *logs.LogOption // nothing to do anymore } }() + go func() { + for { + state, err := c.State() + if err != nil { + until <- time.Time{} + logrus.Error(err) + break + } + time.Sleep(watch.POLL_DURATION) + if state != define.ContainerStateRunning && state != define.ContainerStatePaused { + until <- time.Time{} + break + } + } + }() follower := FollowBuffer{logChannel} err := r.Follow(until, follower) if err != nil { -- cgit v1.2.3-54-g00ecf