mirror of
https://github.com/containers/podman.git
synced 2025-06-21 01:19:15 +08:00
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 <qiwan@redhat.com> Signed-off-by: jgallucci32 <john.gallucci.iv@gmail.com>
This commit is contained in:
@ -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
|
||||
}
|
||||
|
@ -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})
|
||||
logc.WaitWithDefaultTimeout()
|
||||
Expect(logc).To(Exit(0))
|
||||
|
||||
results := podmanTest.Podman([]string{"logs", "-f", containerName})
|
||||
results.WaitWithDefaultTimeout()
|
||||
Expect(results).To(Exit(0))
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user