2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-22 23:32:19 +00:00
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
2022-12-22 23:32:19 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
2017-03-31 03:01:41 +00:00
|
|
|
"net/url"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2016-10-20 22:56:27 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-03-31 03:01:41 +00:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
2022-12-22 23:32:19 +00:00
|
|
|
const containerWaitErrorMsgLimit = 2 * 1024 /* Max: 2KiB */
|
|
|
|
|
2017-06-01 11:21:01 +00:00
|
|
|
// ContainerWait waits until the specified container is in a certain state
|
2017-03-31 03:01:41 +00:00
|
|
|
// indicated by the given condition, either "not-running" (default),
|
|
|
|
// "next-exit", or "removed".
|
|
|
|
//
|
2017-05-21 23:24:07 +00:00
|
|
|
// If this client's API version is before 1.30, condition is ignored and
|
2017-03-31 03:01:41 +00: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 23:24:07 +00:00
|
|
|
// synchronize ContainerWait with other calls, such as specifying a
|
2017-03-31 03:01:41 +00:00
|
|
|
// "next-exit" condition before issuing a ContainerStart request.
|
2022-03-05 16:13:15 +00:00
|
|
|
func (cli *Client) ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.WaitResponse, <-chan error) {
|
2017-03-31 03:01:41 +00:00
|
|
|
if versions.LessThan(cli.ClientVersion(), "1.30") {
|
|
|
|
return cli.legacyContainerWait(ctx, containerID)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 16:13:15 +00:00
|
|
|
resultC := make(chan container.WaitResponse)
|
2017-05-19 06:40:58 +00:00
|
|
|
errC := make(chan error, 1)
|
2017-03-31 03:01:41 +00:00
|
|
|
|
|
|
|
query := url.Values{}
|
2022-02-11 21:54:20 +00:00
|
|
|
if condition != "" {
|
|
|
|
query.Set("condition", string(condition))
|
|
|
|
}
|
2017-03-31 03:01:41 +00:00
|
|
|
|
|
|
|
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 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
go func() {
|
|
|
|
defer ensureReaderClosed(resp)
|
2022-12-22 23:32:19 +00:00
|
|
|
|
|
|
|
body := resp.body
|
|
|
|
responseText := bytes.NewBuffer(nil)
|
|
|
|
stream := io.TeeReader(body, responseText)
|
|
|
|
|
2022-03-05 16:13:15 +00:00
|
|
|
var res container.WaitResponse
|
2022-12-22 23:32:19 +00:00
|
|
|
if err := json.NewDecoder(stream).Decode(&res); err != nil {
|
|
|
|
// NOTE(nicks): The /wait API does not work well with HTTP proxies.
|
|
|
|
// At any time, the proxy could cut off the response stream.
|
|
|
|
//
|
|
|
|
// But because the HTTP status has already been written, the proxy's
|
|
|
|
// only option is to write a plaintext error message.
|
|
|
|
//
|
|
|
|
// If there's a JSON parsing error, read the real error message
|
|
|
|
// off the body and send it to the client.
|
|
|
|
_, _ = io.ReadAll(io.LimitReader(stream, containerWaitErrorMsgLimit))
|
|
|
|
errC <- errors.New(responseText.String())
|
2017-03-31 03:01:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resultC <- res
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resultC, errC
|
|
|
|
}
|
|
|
|
|
|
|
|
// legacyContainerWait returns immediately and doesn't have an option to wait
|
|
|
|
// until the container is removed.
|
2022-03-05 16:13:15 +00:00
|
|
|
func (cli *Client) legacyContainerWait(ctx context.Context, containerID string) (<-chan container.WaitResponse, <-chan error) {
|
|
|
|
resultC := make(chan container.WaitResponse)
|
2017-03-31 03:01:41 +00:00
|
|
|
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)
|
|
|
|
|
2022-03-05 16:13:15 +00:00
|
|
|
var res container.WaitResponse
|
2017-03-31 03:01:41 +00:00
|
|
|
if err := json.NewDecoder(resp.body).Decode(&res); err != nil {
|
|
|
|
errC <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resultC <- res
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resultC, errC
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|