mirror of
https://github.com/containers/podman.git
synced 2025-06-20 00:51:16 +08:00
Merge pull request #6702 from jgallucci32/follow-logs-poll
Stop following logs using timers
This commit is contained in:
@ -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"
|
||||
)
|
||||
@ -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
|
||||
}
|
||||
|
@ -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))
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user