resume waiting for the container to complete
This commit is contained in:
parent
b382b6d4e5
commit
110b9c3dd2
1 changed files with 41 additions and 15 deletions
|
@ -1,9 +1,11 @@
|
||||||
package docker
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/samalba/dockerclient"
|
"github.com/samalba/dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -80,24 +82,48 @@ func Wait(client dockerclient.Client, name string) (*dockerclient.ContainerInfo,
|
||||||
errc := make(chan error, 1)
|
errc := make(chan error, 1)
|
||||||
infoc := make(chan *dockerclient.ContainerInfo, 1)
|
infoc := make(chan *dockerclient.ContainerInfo, 1)
|
||||||
go func() {
|
go func() {
|
||||||
|
// options to fetch the stdout and stderr logs
|
||||||
// blocks and waits for the container to finish
|
// by tailing the output.
|
||||||
// by streaming the logs (to /dev/null). Ideally
|
logOptsTail := &dockerclient.LogOptions{
|
||||||
// we could use the `wait` function instead
|
Follow: true,
|
||||||
rc, err := client.ContainerLogs(name, LogOptsTail)
|
Stdout: true,
|
||||||
if err != nil {
|
Stderr: true,
|
||||||
errc <- err
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
io.Copy(ioutil.Discard, rc)
|
|
||||||
rc.Close()
|
|
||||||
|
|
||||||
info, err := client.InspectContainer(name)
|
for attempts := 0; attempts < 5; attempts++ {
|
||||||
if err != nil {
|
if attempts > 0 {
|
||||||
errc <- err
|
// When resuming the stream, only grab the last line when starting
|
||||||
return
|
// the tailing.
|
||||||
|
logOptsTail.Tail = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// blocks and waits for the container to finish
|
||||||
|
// by streaming the logs (to /dev/null). Ideally
|
||||||
|
// we could use the `wait` function instead
|
||||||
|
rc, err := client.ContainerLogs(name, logOptsTail)
|
||||||
|
if err != nil {
|
||||||
|
errc <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
io.Copy(ioutil.Discard, rc)
|
||||||
|
rc.Close()
|
||||||
|
|
||||||
|
info, err := client.InspectContainer(name)
|
||||||
|
if err != nil {
|
||||||
|
errc <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !info.State.Running {
|
||||||
|
// The container is no longer running, there should be no more logs to tail.
|
||||||
|
infoc <- info
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Attempting to resume log tailing after %d attempts.\n", attempts)
|
||||||
}
|
}
|
||||||
infoc <- info
|
|
||||||
|
errc <- errors.New("Maximum number of attempts made while tailing logs.")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
|
Loading…
Reference in a new issue