2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-03-30 23:01:41 -04:00
|
|
|
"net/url"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-10-20 18:56:27 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-03-30 23:01:41 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
2017-06-01 07:21:01 -04:00
|
|
|
// ContainerWait waits until the specified container is in a certain state
|
2017-03-30 23:01:41 -04:00
|
|
|
// indicated by the given condition, either "not-running" (default),
|
|
|
|
// "next-exit", or "removed".
|
|
|
|
//
|
2017-05-21 19:24:07 -04:00
|
|
|
// If this client's API version is before 1.30, condition is ignored and
|
2017-03-30 23:01:41 -04:00
|
|
|
// 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
|
2017-05-21 19:24:07 -04:00
|
|
|
// synchronize ContainerWait with other calls, such as specifying a
|
2017-03-30 23:01:41 -04:00
|
|
|
// "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)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-03-30 23:01:41 -04:00
|
|
|
resultC := make(chan container.ContainerWaitOKBody)
|
2017-05-19 02:40:58 -04:00
|
|
|
errC := make(chan error, 1)
|
2017-03-30 23:01:41 -04:00
|
|
|
|
|
|
|
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
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
2017-03-30 23:01:41 -04:00
|
|
|
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
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|