Add a StoppedByUser field to the DB

This field indicates that a container was explciitly stopped by
an API call, and did not exit naturally. It's used when
implementing restart policy for containers.

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
This commit is contained in:
Matthew Heon 2019-04-01 13:30:28 -04:00
parent 0eaba2d56a
commit 3fb52f4fbb
2 changed files with 25 additions and 0 deletions

View file

@ -179,6 +179,9 @@ type ContainerState struct {
// This maps the path the file will be mounted to in the container to
// the path of the file on disk outside the container
BindMounts map[string]string `json:"bindMounts,omitempty"`
// StoppedByUser indicates whether the container was stopped by an
// explicit call to the Stop() API.
StoppedByUser bool
// ExtensionStageHooks holds hooks which will be executed by libpod
// and not delegated to the OCI runtime.
@ -1003,6 +1006,21 @@ func (c *Container) BindMounts() (map[string]string, error) {
return newMap, nil
}
// StoppedByUser returns whether the container was last stopped by an explicit
// call to the Stop() API, or whether it exited naturally.
func (c *Container) StoppedByUser() (bool, error) {
if !c.batched {
c.lock.Lock()
defer c.lock.Unlock()
if err := c.syncContainer(); err != nil {
return false, err
}
}
return c.state.StoppedByUser, nil
}
// Misc Accessors
// Most will require locking

View file

@ -376,6 +376,7 @@ func resetState(state *ContainerState) error {
state.ExecSessions = make(map[string]*ExecSession)
state.NetworkStatus = nil
state.BindMounts = make(map[string]string)
state.StoppedByUser = false
return nil
}
@ -789,6 +790,7 @@ func (c *Container) init(ctx context.Context) error {
c.state.ExitCode = 0
c.state.Exited = false
c.state.State = ContainerStateCreated
c.state.StoppedByUser = false
if err := c.save(); err != nil {
return err
@ -950,6 +952,11 @@ func (c *Container) stop(timeout uint) error {
return err
}
c.state.StoppedByUser = true
if err := c.save(); err != nil {
return errors.Wrapf(err, "error saving container %s state after stopping", c.ID())
}
// Wait until we have an exit file, and sync once we do
return c.waitForExitFileAndSync()
}