use a single callback

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-03-05 17:04:43 -08:00
parent e0fa132ec9
commit 0e7f970d4d
3 changed files with 32 additions and 11 deletions

View File

@ -263,13 +263,14 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) {
if tag := r.Form.Get("tag"); tag != "" {
image += ":" + tag
}
begin := func(name string) {
fmt.Fprintf(wf, "{%q:%q,%q:\"Pulling %s...\",%q:{}}", "id", name, "status", image, "progressDetail")
callback := func(what, status string) {
if status == "" {
fmt.Fprintf(wf, "{%q:%q,%q:\"Pulling %s...\",%q:{}}", "id", what, "status", image, "progressDetail")
} else {
fmt.Fprintf(wf, "{%q:%q,%q:\"Pulling %s... : %s\",%q:{}}", "id", what, "status", image, status, "progressDetail")
}
}
end := func(name string) {
fmt.Fprintf(wf, "{%q:%q,%q:\"Pulling %s... : downloaded\",%q:{}}", "id", name, "status", image, "progressDetail")
}
c.cluster.Pull(image, begin, end)
c.cluster.Pull(image, callback)
} else { //import
httpError(w, "Not supported in clustering mode.", http.StatusNotImplemented)
}

View File

@ -3,15 +3,31 @@ package cluster
import "github.com/samalba/dockerclient"
type Cluster interface {
// Create a container
CreateContainer(config *dockerclient.ContainerConfig, name string) (*Container, error)
// Remove a container
RemoveContainer(container *Container, force bool) error
// Return all images
Images() []*Image
// Return one image matching `IdOrName`
Image(IdOrName string) *Image
// Return all containers
Containers() []*Container
// Return container the matching `IdOrName`
Container(IdOrName string) *Container
Pull(name string, begin, end func(string))
// Pull images
// `callback` can be called multiple time
// `what` is what is being pulled
// `status` is the current status, like "", "in progress" or "downloaded
Pull(name string, callback func(what, status string))
// Return some info about the cluster, like nb or containers / images
// It is pretty open, so the implementation decides what to return.
Info() [][2]string
}

View File

@ -186,14 +186,18 @@ func (c *Cluster) Image(IdOrName string) *cluster.Image {
return nil
}
func (c *Cluster) Pull(name string, begin, end func(string)) {
func (c *Cluster) Pull(name string, callback func(what, status string)) {
size := len(c.nodes)
done := make(chan bool, size)
for _, n := range c.nodes {
go func(nn *node) {
begin(nn.Name())
nn.Pull(name)
end(nn.Name())
if callback != nil {
callback(nn.Name(), "")
}
nn.pull(name)
if callback != nil {
callback(nn.Name(), "downloaded")
}
done <- true
}(n)
}