Browse code

Merge pull request #415 from dotcloud/261-choose_grace_period-feature

added -t in docker stop and restart to choose grace period

Guillaume J. Charmes authored on 2013/04/20 04:42:34
Showing 4 changed files
... ...
@@ -223,7 +223,8 @@ func (srv *Server) CmdInfo(stdin io.ReadCloser, stdout io.Writer, args ...string
223 223
 }
224 224
 
225 225
 func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
226
-	cmd := rcli.Subcmd(stdout, "stop", "CONTAINER [CONTAINER...]", "Stop a running container")
226
+	cmd := rcli.Subcmd(stdout, "stop", "[OPTIONS] CONTAINER [CONTAINER...]", "Stop a running container")
227
+	nSeconds := cmd.Int("t", 10, "wait t seconds before killing the container")
227 228
 	if err := cmd.Parse(args); err != nil {
228 229
 		return nil
229 230
 	}
... ...
@@ -233,7 +234,7 @@ func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string
233 233
 	}
234 234
 	for _, name := range cmd.Args() {
235 235
 		if container := srv.runtime.Get(name); container != nil {
236
-			if err := container.Stop(); err != nil {
236
+			if err := container.Stop(*nSeconds); err != nil {
237 237
 				return err
238 238
 			}
239 239
 			fmt.Fprintln(stdout, container.ShortId())
... ...
@@ -246,6 +247,7 @@ func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string
246 246
 
247 247
 func (srv *Server) CmdRestart(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
248 248
 	cmd := rcli.Subcmd(stdout, "restart", "CONTAINER [CONTAINER...]", "Restart a running container")
249
+	nSeconds := cmd.Int("t", 10, "wait t seconds before killing the container")
249 250
 	if err := cmd.Parse(args); err != nil {
250 251
 		return nil
251 252
 	}
... ...
@@ -255,7 +257,7 @@ func (srv *Server) CmdRestart(stdin io.ReadCloser, stdout io.Writer, args ...str
255 255
 	}
256 256
 	for _, name := range cmd.Args() {
257 257
 		if container := srv.runtime.Get(name); container != nil {
258
-			if err := container.Restart(); err != nil {
258
+			if err := container.Restart(*nSeconds); err != nil {
259 259
 				return err
260 260
 			}
261 261
 			fmt.Fprintln(stdout, container.ShortId())
... ...
@@ -600,7 +600,7 @@ func (container *Container) Kill() error {
600 600
 	return container.kill()
601 601
 }
602 602
 
603
-func (container *Container) Stop() error {
603
+func (container *Container) Stop(seconds int) error {
604 604
 	container.State.lock()
605 605
 	defer container.State.unlock()
606 606
 	if !container.State.Running {
... ...
@@ -620,8 +620,8 @@ func (container *Container) Stop() error {
620 620
 	}
621 621
 
622 622
 	// 2. Wait for the process to exit on its own
623
-	if err := container.WaitTimeout(10 * time.Second); err != nil {
624
-		log.Printf("Container %v failed to exit within 10 seconds of SIGTERM - using the force", container.Id)
623
+	if err := container.WaitTimeout(time.Duration(seconds) * time.Second); err != nil {
624
+		log.Printf("Container %v failed to exit within %d seconds of SIGTERM - using the force", container.Id, seconds)
625 625
 		if err := container.kill(); err != nil {
626 626
 			return err
627 627
 		}
... ...
@@ -629,8 +629,8 @@ func (container *Container) Stop() error {
629 629
 	return nil
630 630
 }
631 631
 
632
-func (container *Container) Restart() error {
633
-	if err := container.Stop(); err != nil {
632
+func (container *Container) Restart(seconds int) error {
633
+	if err := container.Stop(seconds); err != nil {
634 634
 		return err
635 635
 	}
636 636
 	if err := container.Start(); err != nil {
... ...
@@ -97,7 +97,7 @@ func TestMultipleAttachRestart(t *testing.T) {
97 97
 		t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
98 98
 	}
99 99
 
100
-	if err := container.Stop(); err != nil {
100
+	if err := container.Stop(10); err != nil {
101 101
 		t.Fatal(err)
102 102
 	}
103 103
 
... ...
@@ -217,7 +217,7 @@ func (runtime *Runtime) Destroy(container *Container) error {
217 217
 		return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.Id)
218 218
 	}
219 219
 
220
-	if err := container.Stop(); err != nil {
220
+	if err := container.Stop(10); err != nil {
221 221
 		return err
222 222
 	}
223 223
 	if mounted, err := container.Mounted(); err != nil {