Jelajahi Sumber

Improve wait for lxc and driver interface

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
Michael Crosby 11 tahun lalu
induk
melakukan
8c9f62d037
5 mengubah file dengan 14 tambahan dan 42 penghapusan
  1. 1 1
      container.go
  2. 2 3
      execdriver/chroot/driver.go
  3. 1 2
      execdriver/driver.go
  4. 8 34
      execdriver/lxc/driver.go
  5. 2 2
      runtime.go

+ 1 - 1
container.go

@@ -1115,7 +1115,7 @@ func (container *Container) monitor(callback execdriver.StartCallback) error {
 
 	if container.process == nil {
 		// This happends when you have a GHOST container with lxc
-		err = container.runtime.Wait(container, 0)
+		err = container.runtime.WaitGhost(container)
 	} else {
 		exitCode, err = container.runtime.Run(container, callback)
 	}

+ 2 - 3
execdriver/chroot/driver.go

@@ -3,7 +3,6 @@ package chroot
 import (
 	"github.com/dotcloud/docker/execdriver"
 	"os/exec"
-	"time"
 )
 
 type driver struct {
@@ -55,8 +54,8 @@ func (d *driver) Kill(p *execdriver.Process, sig int) error {
 	return p.Process.Kill()
 }
 
-func (d *driver) Wait(id string, duration time.Duration) error {
-	panic("No Implemented")
+func (d *driver) Wait(id string) error {
+	panic("Not Implemented")
 }
 
 func (d *driver) Version() string {

+ 1 - 2
execdriver/driver.go

@@ -4,7 +4,6 @@ import (
 	"errors"
 	"os/exec"
 	"syscall"
-	"time"
 )
 
 var (
@@ -18,7 +17,7 @@ type Driver interface {
 	Run(c *Process, startCallback StartCallback) (int, error) // Run executes the process and blocks until the process exits and returns the exit code
 	Kill(c *Process, sig int) error
 	// TODO: @crosbymichael @creack wait should probably return the exit code
-	Wait(id string, duration time.Duration) error // Wait on an out of process...process - lxc ghosts
+	Wait(id string) error // Wait on an out of process...process - lxc ghosts
 	Version() string
 	Name() string
 }

+ 8 - 34
execdriver/lxc/driver.go

@@ -133,24 +133,17 @@ func (d *driver) Kill(c *execdriver.Process, sig int) error {
 	return d.kill(c, sig)
 }
 
-func (d *driver) Wait(id string, duration time.Duration) error {
-	var (
-		killer bool
-		done   = d.waitLxc(id, &killer)
-	)
-
-	if duration > 0 {
-		select {
-		case err := <-done:
+func (d *driver) Wait(id string) error {
+	for {
+		output, err := exec.Command("lxc-info", "-n", id).CombinedOutput()
+		if err != nil {
 			return err
-		case <-time.After(duration):
-			killer = true
-			return execdriver.ErrWaitTimeoutReached
 		}
-	} else {
-		return <-done
+		if !strings.Contains(string(output), "RUNNING") {
+			return nil
+		}
+		time.Sleep(500 * time.Millisecond)
 	}
-	return nil
 }
 
 func (d *driver) Version() string {
@@ -207,25 +200,6 @@ func (d *driver) waitForStart(c *execdriver.Process, waitLock chan struct{}) err
 	return execdriver.ErrNotRunning
 }
 
-func (d *driver) waitLxc(id string, kill *bool) <-chan error {
-	done := make(chan error)
-	go func() {
-		for *kill {
-			output, err := exec.Command("lxc-info", "-n", id).CombinedOutput()
-			if err != nil {
-				done <- err
-				return
-			}
-			if !strings.Contains(string(output), "RUNNING") {
-				done <- err
-				return
-			}
-			time.Sleep(500 * time.Millisecond)
-		}
-	}()
-	return done
-}
-
 func (d *driver) getInfo(c *execdriver.Process) ([]byte, error) {
 	return exec.Command("lxc-info", "-s", "-n", c.ID).CombinedOutput()
 }

+ 2 - 2
runtime.go

@@ -849,8 +849,8 @@ func (runtime *Runtime) Kill(c *Container, sig int) error {
 	return runtime.execDriver.Kill(c.process, sig)
 }
 
-func (runtime *Runtime) Wait(c *Container, duration time.Duration) error {
-	return runtime.execDriver.Wait(c.ID, duration)
+func (runtime *Runtime) WaitGhost(c *Container) error {
+	return runtime.execDriver.Wait(c.ID)
 }
 
 // Nuke kills all containers then removes all content