mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-29 21:31:02 +00:00
use dockerclient.Wait to block for in-progress builds
This commit is contained in:
parent
8df397db9f
commit
7a49d0324b
1 changed files with 12 additions and 50 deletions
|
@ -2,8 +2,6 @@ package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/samalba/dockerclient"
|
"github.com/samalba/dockerclient"
|
||||||
|
@ -79,57 +77,21 @@ func Wait(client dockerclient.Client, name string) (*dockerclient.ContainerInfo,
|
||||||
client.KillContainer(name, "9")
|
client.KillContainer(name, "9")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
errc := make(chan error, 1)
|
for attempts := 0; attempts < 5; attempts++ {
|
||||||
infoc := make(chan *dockerclient.ContainerInfo, 1)
|
done := client.Wait(name)
|
||||||
go func() {
|
<-done
|
||||||
// options to fetch the stdout and stderr logs
|
|
||||||
// by tailing the output.
|
info, err := client.InspectContainer(name)
|
||||||
logOptsTail := &dockerclient.LogOptions{
|
if err != nil {
|
||||||
Follow: true,
|
return nil, err
|
||||||
Stdout: true,
|
|
||||||
Stderr: true,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for attempts := 0; attempts < 5; attempts++ {
|
if !info.State.Running {
|
||||||
if attempts > 0 {
|
return info, nil
|
||||||
// When resuming the stream, only grab the last line when starting
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errc <- errors.New("Maximum number of attempts made while tailing logs.")
|
log.Debugf("attempting to resume waiting after %d attempts.\n", attempts)
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case info := <-infoc:
|
|
||||||
return info, nil
|
|
||||||
case err := <-errc:
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("reached maximum wait attempts")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue