Skip to content

Commit

Permalink
Merge pull request #6702 from jgallucci32/follow-logs-poll
Browse files Browse the repository at this point in the history
Stop following logs using timers
  • Loading branch information
openshift-merge-robot authored Jun 22, 2020
2 parents 78b205c + 173d086 commit 11dd5f5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions libpod/container_log.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package libpod

import (
"fmt"
"os"
"time"

"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"
)
Expand Down Expand Up @@ -81,5 +84,31 @@ 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()
time.Sleep(watch.POLL_DURATION)
if err != nil {
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 {
tailError := t.StopAtEOF()
if tailError != nil && fmt.Sprintf("%v", tailError) != "tail: stop at eof" {
logrus.Error(tailError)
}
break
}
}
}()
}
return nil
}
12 changes: 12 additions & 0 deletions test/e2e/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,16 @@ var _ = Describe("Podman logs", func() {
logs.WaitWithDefaultTimeout()
Expect(logs).To(Not(Exit(0)))
})

It("follow output stopped container", func() {
containerName := "logs-f"

logc := podmanTest.Podman([]string{"run", "--name", containerName, "-d", ALPINE, "true"})
logc.WaitWithDefaultTimeout()
Expect(logc).To(Exit(0))

results := podmanTest.Podman([]string{"logs", "-f", containerName})
results.WaitWithDefaultTimeout()
Expect(results).To(Exit(0))
})
})

0 comments on commit 11dd5f5

Please sign in to comment.