Merge pull request #1482 from macb/docker/resume_wait

resume waiting for the container to complete
This commit is contained in:
Brad Rydzewski 2016-02-12 11:21:09 -08:00
commit 19a7ae53e6
2 changed files with 18 additions and 25 deletions

View file

@ -329,6 +329,11 @@ func (e *engine) runJob(c context.Context, r *Task, updater *updater, client doc
info, builderr := docker.Wait(client, name) info, builderr := docker.Wait(client, name)
switch { switch {
case info.State.Running:
// A build unblocked before actually being completed.
log.Errorf("incomplete build: %s", name)
r.Job.ExitCode = 1
r.Job.Status = model.StatusError
case info.State.ExitCode == 128: case info.State.ExitCode == 128:
r.Job.ExitCode = info.State.ExitCode r.Job.ExitCode = info.State.ExitCode
r.Job.Status = model.StatusKilled r.Job.Status = model.StatusKilled

View file

@ -1,9 +1,9 @@
package docker package docker
import ( import (
"io" "errors"
"io/ioutil"
log "github.com/Sirupsen/logrus"
"github.com/samalba/dockerclient" "github.com/samalba/dockerclient"
) )
@ -77,33 +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
// 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) info, err := client.InspectContainer(name)
if err != nil { if err != nil {
errc <- err return nil, err
return
} }
infoc <- info
}()
select { if !info.State.Running {
case info := <-infoc: return info, nil
return info, nil }
case err := <-errc:
return nil, err log.Debugf("attempting to resume waiting after %d attempts.\n", attempts)
} }
return nil, errors.New("reached maximum wait attempts")
} }