1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Decouple daemon and container to pause and unpause containers.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-11-02 18:39:39 -05:00
parent 4f2a5ba360
commit 9f79cfdb2f
5 changed files with 55 additions and 51 deletions

View file

@ -22,8 +22,8 @@ type ContainerCommitConfig struct {
// The image can optionally be tagged into a repository.
func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) {
if c.Pause && !container.isPaused() {
container.pause()
defer container.unpause()
daemon.containerPause(container)
defer daemon.containerUnpause(container)
}
rwTar, err := daemon.exportContainerRw(container)

View file

@ -343,50 +343,6 @@ func (container *Container) ExitOnNext() {
container.monitor.ExitOnNext()
}
func (container *Container) pause() error {
container.Lock()
defer container.Unlock()
// We cannot Pause the container which is not running
if !container.Running {
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
}
// We cannot Pause the container which is already paused
if container.Paused {
return derr.ErrorCodeAlreadyPaused.WithArgs(container.ID)
}
if err := container.daemon.execDriver.Pause(container.command); err != nil {
return err
}
container.Paused = true
container.logEvent("pause")
return nil
}
func (container *Container) unpause() error {
container.Lock()
defer container.Unlock()
// We cannot unpause the container which is not running
if !container.Running {
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
}
// We cannot unpause the container which is not paused
if !container.Paused {
return derr.ErrorCodeNotPaused.WithArgs(container.ID)
}
if err := container.daemon.execDriver.Unpause(container.command); err != nil {
return err
}
container.Paused = false
container.logEvent("unpause")
return nil
}
// Resize changes the TTY of the process running inside the container
// to the given height and width. The container must be running.
func (container *Container) Resize(h, w int) error {

View file

@ -849,10 +849,10 @@ func (daemon *Daemon) shutdownContainer(c *Container) error {
if !ok {
return fmt.Errorf("System doesn not support SIGTERM")
}
if err := c.daemon.kill(c, int(sig)); err != nil {
if err := daemon.kill(c, int(sig)); err != nil {
return fmt.Errorf("sending SIGTERM to container %s with error: %v", c.ID, err)
}
if err := c.unpause(); err != nil {
if err := daemon.containerUnpause(c); err != nil {
return fmt.Errorf("Failed to unpause container %s with error: %v", c.ID, err)
}
if _, err := c.WaitStop(10 * time.Second); err != nil {
@ -861,7 +861,7 @@ func (daemon *Daemon) shutdownContainer(c *Container) error {
if !ok {
return fmt.Errorf("System does not support SIGKILL")
}
if err := c.daemon.kill(c, int(sig)); err != nil {
if err := daemon.kill(c, int(sig)); err != nil {
logrus.Errorf("Failed to SIGKILL container %s", c.ID)
}
c.WaitStop(-1 * time.Second)

View file

@ -11,9 +11,33 @@ func (daemon *Daemon) ContainerPause(name string) error {
return err
}
if err := container.pause(); err != nil {
if err := daemon.containerPause(container); err != nil {
return derr.ErrorCodePauseError.WithArgs(name, err)
}
return nil
}
// containerPause pauses the container execution without stopping the process.
// The execution can be resumed by calling containerUnpause.
func (daemon *Daemon) containerPause(container *Container) error {
container.Lock()
defer container.Unlock()
// We cannot Pause the container which is not running
if !container.Running {
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
}
// We cannot Pause the container which is already paused
if container.Paused {
return derr.ErrorCodeAlreadyPaused.WithArgs(container.ID)
}
if err := daemon.execDriver.Pause(container.command); err != nil {
return err
}
container.Paused = true
daemon.logContainerEvent(container, "pause")
return nil
}

View file

@ -11,9 +11,33 @@ func (daemon *Daemon) ContainerUnpause(name string) error {
return err
}
if err := container.unpause(); err != nil {
if err := daemon.containerUnpause(container); err != nil {
return derr.ErrorCodeCantUnpause.WithArgs(name, err)
}
return nil
}
// containerUnpause resumes the container execution after the container is paused.
func (daemon *Daemon) containerUnpause(container *Container) error {
container.Lock()
defer container.Unlock()
// We cannot unpause the container which is not running
if !container.Running {
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
}
// We cannot unpause the container which is not paused
if !container.Paused {
return derr.ErrorCodeNotPaused.WithArgs(container.ID)
}
if err := daemon.execDriver.Unpause(container.command); err != nil {
return err
}
container.Paused = false
daemon.logContainerEvent(container, "unpause")
return nil
}