Fix journald logs --follow

Previously, --follow with a podman logs using journald would not exit

Signed-off-by: Ashley Cui <acui@redhat.com>
This commit is contained in:
Ashley Cui 2021-02-17 15:02:22 -05:00
parent f2d057c943
commit 9016387bba
1 changed files with 22 additions and 1 deletions

View File

@ -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 {