diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2018-09-21 09:40:30 -0400 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2018-09-21 20:07:51 +0000 |
commit | 9e81f9daa4af9802088530a35a72814172430a36 (patch) | |
tree | e8ae1239d00c83f22a741662852e4dcad34fb2c4 | |
parent | 785e9ea1fd985713db1aa8cc6bec07805faef7a2 (diff) | |
download | podman-9e81f9daa4af9802088530a35a72814172430a36.tar.gz podman-9e81f9daa4af9802088530a35a72814172430a36.tar.bz2 podman-9e81f9daa4af9802088530a35a72814172430a36.zip |
Refactor Wait() to not require a timeout
We added a timeout for convenience, but most invocations don't
care about it. Refactor it into WaitWithTimeout() and add a
Wait() that doesn't require a timeout and uses the default.
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Closes: #1527
Approved by: mheon
-rw-r--r-- | cmd/podman/run.go | 2 | ||||
-rw-r--r-- | cmd/podman/start.go | 2 | ||||
-rw-r--r-- | cmd/podman/wait.go | 5 | ||||
-rw-r--r-- | libpod/container.go | 6 | ||||
-rw-r--r-- | libpod/container_api.go | 13 | ||||
-rw-r--r-- | pkg/varlinkapi/containers.go | 2 |
6 files changed, 19 insertions, 11 deletions
diff --git a/cmd/podman/run.go b/cmd/podman/run.go index ce7d0c40f..f9a96e4a6 100644 --- a/cmd/podman/run.go +++ b/cmd/podman/run.go @@ -121,7 +121,7 @@ func runCmd(c *cli.Context) error { return err } - if ecode, err := ctr.Wait(libpod.WaitTimeout); err != nil { + if ecode, err := ctr.Wait(); err != nil { if errors.Cause(err) == libpod.ErrNoSuchCtr { // The container may have been removed // Go looking for an exit file diff --git a/cmd/podman/start.go b/cmd/podman/start.go index a80d0e1e8..cb65ec6d4 100644 --- a/cmd/podman/start.go +++ b/cmd/podman/start.go @@ -115,7 +115,7 @@ func startCmd(c *cli.Context) error { return errors.Wrapf(err, "unable to start container %s", ctr.ID()) } - if ecode, err := ctr.Wait(libpod.WaitTimeout); err != nil { + if ecode, err := ctr.Wait(); err != nil { logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err) } else { exitCode = int(ecode) diff --git a/cmd/podman/wait.go b/cmd/podman/wait.go index 48d3885e7..07db20eee 100644 --- a/cmd/podman/wait.go +++ b/cmd/podman/wait.go @@ -6,7 +6,6 @@ import ( "time" "github.com/containers/libpod/cmd/podman/libpodruntime" - "github.com/containers/libpod/libpod" "github.com/pkg/errors" "github.com/urfave/cli" ) @@ -21,7 +20,7 @@ var ( cli.UintFlag{ Name: "interval, i", Usage: "Milliseconds to wait before polling for completion", - Value: uint(libpod.WaitTimeout), + Value: 250, }, LatestFlag, } @@ -69,7 +68,7 @@ func waitCmd(c *cli.Context) error { if c.Uint("interval") == 0 { return errors.Errorf("interval must be greater then 0") } - returnCode, err := ctr.Wait(time.Duration(c.Uint("interval"))) + returnCode, err := ctr.WaitWithInterval(time.Duration(c.Uint("interval")) * time.Millisecond) if err != nil { if lastError != nil { fmt.Fprintln(os.Stderr, lastError) diff --git a/libpod/container.go b/libpod/container.go index fc613f406..0b1879208 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -36,8 +36,6 @@ const ( ContainerStateStopped ContainerStatus = iota // ContainerStatePaused indicates that the container has been paused ContainerStatePaused ContainerStatus = iota - // WaitTimeout is the wait timeout before checking for container exit - WaitTimeout = time.Second / time.Millisecond ) // CgroupfsDefaultCgroupParent is the cgroup parent for CGroupFS in libpod @@ -47,6 +45,10 @@ const CgroupfsDefaultCgroupParent = "/libpod_parent" // manager in libpod const SystemdDefaultCgroupParent = "machine.slice" +// DefaultWaitInterval is the default interval between container status checks +// while waiting. +const DefaultWaitInterval = 250 * time.Millisecond + // LinuxNS represents a Linux namespace type LinuxNS int diff --git a/libpod/container_api.go b/libpod/container_api.go index 76537ebc4..fc2058de6 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -593,13 +593,20 @@ func (c *Container) Inspect(size bool) (*inspect.ContainerInspectData, error) { return c.getContainerInspectData(size, driverData) } -// Wait blocks on a container to exit and returns its exit code -func (c *Container) Wait(waitTimeout time.Duration) (int32, error) { +// Wait blocks until the container exits and returns its exit code. +func (c *Container) Wait() (int32, error) { + return c.WaitWithInterval(DefaultWaitInterval) +} + +// WaitWithInterval blocks until the container to exit and returns its exit +// code. The argument is the interval at which checks the container's status. +func (c *Container) WaitWithInterval(waitTimeout time.Duration) (int32, error) { if !c.valid { return -1, ErrCtrRemoved } - err := wait.PollImmediateInfinite(waitTimeout*time.Millisecond, + err := wait.PollImmediateInfinite(waitTimeout, func() (bool, error) { + logrus.Debugf("Checking container %s status...", c.ID()) stopped, err := c.isStopped() if err != nil { return false, err diff --git a/pkg/varlinkapi/containers.go b/pkg/varlinkapi/containers.go index de9c23034..f517e9b6e 100644 --- a/pkg/varlinkapi/containers.go +++ b/pkg/varlinkapi/containers.go @@ -341,7 +341,7 @@ func (i *LibpodAPI) WaitContainer(call iopodman.VarlinkCall, name string) error if err != nil { return call.ReplyContainerNotFound(name) } - exitCode, err := ctr.Wait(libpod.WaitTimeout) + exitCode, err := ctr.Wait() if err != nil { return call.ReplyErrorOccurred(err.Error()) } |