From 1615bb08c7c3fc6c4b22db0a633edda516f97cf0 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 16 Apr 2013 18:43:44 +0200 Subject: [PATCH 1/2] added -t in docker stop and restart to choose grace period --- commands.go | 8 +++++--- container.go | 10 +++++----- runtime.go | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/commands.go b/commands.go index 82a0ce103d..2b0c91a586 100644 --- a/commands.go +++ b/commands.go @@ -223,7 +223,8 @@ func (srv *Server) CmdInfo(stdin io.ReadCloser, stdout io.Writer, args ...string } func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string) error { - cmd := rcli.Subcmd(stdout, "stop", "CONTAINER [CONTAINER...]", "Stop a running container") + cmd := rcli.Subcmd(stdout, "stop", "[OPTIONS] CONTAINER [CONTAINER...]", "Stop a running container") + nSeconds := cmd.Int("t", 10, "wait t seconds before killing the container") if err := cmd.Parse(args); err != nil { return nil } @@ -233,7 +234,7 @@ func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string } for _, name := range cmd.Args() { if container := srv.runtime.Get(name); container != nil { - if err := container.Stop(); err != nil { + if err := container.Stop(*nSeconds); err != nil { return err } fmt.Fprintln(stdout, container.ShortId()) @@ -246,6 +247,7 @@ func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string func (srv *Server) CmdRestart(stdin io.ReadCloser, stdout io.Writer, args ...string) error { cmd := rcli.Subcmd(stdout, "restart", "CONTAINER [CONTAINER...]", "Restart a running container") + nSeconds := cmd.Int("t", 10, "wait t seconds before killing the container") if err := cmd.Parse(args); err != nil { return nil } @@ -255,7 +257,7 @@ func (srv *Server) CmdRestart(stdin io.ReadCloser, stdout io.Writer, args ...str } for _, name := range cmd.Args() { if container := srv.runtime.Get(name); container != nil { - if err := container.Restart(); err != nil { + if err := container.Restart(*nSeconds); err != nil { return err } fmt.Fprintln(stdout, container.ShortId()) diff --git a/container.go b/container.go index 74706a4079..2bd56180a7 100644 --- a/container.go +++ b/container.go @@ -599,7 +599,7 @@ func (container *Container) Kill() error { return container.kill() } -func (container *Container) Stop() error { +func (container *Container) Stop(seconds int) error { container.State.lock() defer container.State.unlock() if !container.State.Running { @@ -619,8 +619,8 @@ func (container *Container) Stop() error { } // 2. Wait for the process to exit on its own - if err := container.WaitTimeout(10 * time.Second); err != nil { - log.Printf("Container %v failed to exit within 10 seconds of SIGTERM - using the force", container.Id) + if err := container.WaitTimeout(time.Duration(seconds) * time.Second); err != nil { + log.Printf("Container %v failed to exit within %d seconds of SIGTERM - using the force", container.Id, seconds) if err := container.kill(); err != nil { return err } @@ -628,8 +628,8 @@ func (container *Container) Stop() error { return nil } -func (container *Container) Restart() error { - if err := container.Stop(); err != nil { +func (container *Container) Restart(seconds int) error { + if err := container.Stop(seconds); err != nil { return err } if err := container.Start(); err != nil { diff --git a/runtime.go b/runtime.go index 3fe07c7ea6..0955eb8703 100644 --- a/runtime.go +++ b/runtime.go @@ -217,7 +217,7 @@ func (runtime *Runtime) Destroy(container *Container) error { return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.Id) } - if err := container.Stop(); err != nil { + if err := container.Stop(10); err != nil { return err } if mounted, err := container.Mounted(); err != nil { From 90602ab62a4dddadff6671d80815465556a267ee Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 18 Apr 2013 16:03:50 +0200 Subject: [PATCH 2/2] fixed test --- container_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container_test.go b/container_test.go index d5f3694c59..9e17390116 100644 --- a/container_test.go +++ b/container_test.go @@ -97,7 +97,7 @@ func TestMultipleAttachRestart(t *testing.T) { t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3) } - if err := container.Stop(); err != nil { + if err := container.Stop(10); err != nil { t.Fatal(err) }