Browse code

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

Victor Vieux authored on 2013/04/17 01:43:44
Showing 3 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())
... ...
@@ -599,7 +599,7 @@ func (container *Container) Kill() error {
599 599
 	return container.kill()
600 600
 }
601 601
 
602
-func (container *Container) Stop() error {
602
+func (container *Container) Stop(seconds int) error {
603 603
 	container.State.lock()
604 604
 	defer container.State.unlock()
605 605
 	if !container.State.Running {
... ...
@@ -619,8 +619,8 @@ func (container *Container) Stop() error {
619 619
 	}
620 620
 
621 621
 	// 2. Wait for the process to exit on its own
622
-	if err := container.WaitTimeout(10 * time.Second); err != nil {
623
-		log.Printf("Container %v failed to exit within 10 seconds of SIGTERM - using the force", container.Id)
622
+	if err := container.WaitTimeout(time.Duration(seconds) * time.Second); err != nil {
623
+		log.Printf("Container %v failed to exit within %d seconds of SIGTERM - using the force", container.Id, seconds)
624 624
 		if err := container.kill(); err != nil {
625 625
 			return err
626 626
 		}
... ...
@@ -628,8 +628,8 @@ func (container *Container) Stop() error {
628 628
 	return nil
629 629
 }
630 630
 
631
-func (container *Container) Restart() error {
632
-	if err := container.Stop(); err != nil {
631
+func (container *Container) Restart(seconds int) error {
632
+	if err := container.Stop(seconds); err != nil {
633 633
 		return err
634 634
 	}
635 635
 	if err := container.Start(); err != nil {
... ...
@@ -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 {