Also bringing: . golang.org/x/net 5561cd9b4330353950f399814f427425c0a26fd2 . github.com/docker/distribution 83389a148052d74ac602f5f1d62f86ff2f3c4aa5 . github.com/docker/swarmkit bd69f6e8e301645afd344913fa1ede53a0a111fb . github.com/docker/go-metrics d466d4f6fd960e01820085bd7e1a24426ee7ef18 . github.com/prometheus/client_golang 52437c81da6b127a9925d17eb3a382a2e5fd395e . github.com/beorn7/perks 4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9 . github.com/prometheus/client_model fa8ad6fec33561be4280a8f0514318c79d7f6cb6 . github.com/prometheus/common ebdfc6da46522d58825777cf1f90490a5b1ef1d8 . github.com/prometheus/procfs abf152e5f3e97f2fafac028d2cc06c1feb87ffa5 . github.com/matttproud/golang_protobuf_extensions v1.0.0 Signed-off-by: Mathieu Champlon <mathieu.champlon@docker.com>
84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package client // import "github.com/docker/docker/client"
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/url"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/api/types/versions"
|
|
)
|
|
|
|
// ContainerWait waits until the specified container is in a certain state
|
|
// indicated by the given condition, either "not-running" (default),
|
|
// "next-exit", or "removed".
|
|
//
|
|
// If this client's API version is before 1.30, condition is ignored and
|
|
// ContainerWait will return immediately with the two channels, as the server
|
|
// will wait as if the condition were "not-running".
|
|
//
|
|
// If this client's API version is at least 1.30, ContainerWait blocks until
|
|
// the request has been acknowledged by the server (with a response header),
|
|
// then returns two channels on which the caller can wait for the exit status
|
|
// of the container or an error if there was a problem either beginning the
|
|
// wait request or in getting the response. This allows the caller to
|
|
// synchronize ContainerWait with other calls, such as specifying a
|
|
// "next-exit" condition before issuing a ContainerStart request.
|
|
func (cli *Client) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) {
|
|
if versions.LessThan(cli.ClientVersion(), "1.30") {
|
|
return cli.legacyContainerWait(ctx, containerID)
|
|
}
|
|
|
|
resultC := make(chan container.ContainerWaitOKBody)
|
|
errC := make(chan error, 1)
|
|
|
|
query := url.Values{}
|
|
query.Set("condition", string(condition))
|
|
|
|
resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", query, nil, nil)
|
|
if err != nil {
|
|
defer ensureReaderClosed(resp)
|
|
errC <- err
|
|
return resultC, errC
|
|
}
|
|
|
|
go func() {
|
|
defer ensureReaderClosed(resp)
|
|
var res container.ContainerWaitOKBody
|
|
if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
|
|
errC <- err
|
|
return
|
|
}
|
|
|
|
resultC <- res
|
|
}()
|
|
|
|
return resultC, errC
|
|
}
|
|
|
|
// legacyContainerWait returns immediately and doesn't have an option to wait
|
|
// until the container is removed.
|
|
func (cli *Client) legacyContainerWait(ctx context.Context, containerID string) (<-chan container.ContainerWaitOKBody, <-chan error) {
|
|
resultC := make(chan container.ContainerWaitOKBody)
|
|
errC := make(chan error)
|
|
|
|
go func() {
|
|
resp, err := cli.post(ctx, "/containers/"+containerID+"/wait", nil, nil, nil)
|
|
if err != nil {
|
|
errC <- err
|
|
return
|
|
}
|
|
defer ensureReaderClosed(resp)
|
|
|
|
var res container.ContainerWaitOKBody
|
|
if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
|
|
errC <- err
|
|
return
|
|
}
|
|
|
|
resultC <- res
|
|
}()
|
|
|
|
return resultC, errC
|
|
}
|