From e3251a24ecc178fecbd9639019274fc382e365d7 Mon Sep 17 00:00:00 2001 From: David Dollar Date: Sun, 31 Jul 2016 22:48:00 -0400 Subject: [PATCH] try a few times to find the build host --- api/controllers/builds.go | 58 ++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/api/controllers/builds.go b/api/controllers/builds.go index 96dc27d40a..0d11fc4ca2 100644 --- a/api/controllers/builds.go +++ b/api/controllers/builds.go @@ -249,32 +249,13 @@ func BuildLogs(ws *websocket.Conn) *httperr.Error { // in production loop through docker hosts that the rack is running on // to find the build - if os.Getenv("DEVELOPMENT") != "true" { - pss, err := models.ListProcesses(os.Getenv("RACK")) - + if os.Getenv("DEVELOPMENT") != "truee" { + h, err := findBuildHost(build) if err != nil { return httperr.Server(err) } - for _, ps := range pss { - client, err := ps.Docker() - - if err != nil { - return httperr.Server(err) - } - - res, err := client.ListContainers(docker.ListContainersOptions{ - All: true, - Filters: map[string][]string{ - "name": []string{fmt.Sprintf("build-%s", build)}, - }, - }) - - if len(res) > 0 { - host = fmt.Sprintf("http://%s:2376", ps.Host) - break - } - } + host = h } // proxy to docker container logs @@ -341,6 +322,39 @@ ForLoop: return httperr.Server(err) } +// try to find the docker host that's running a build +// try a few times with a sleep +func findBuildHost(build string) (string, error) { + for i := 1; i < 5; i++ { + pss, err := models.ListProcesses(os.Getenv("RACK")) + if err != nil { + return "", httperr.Server(err) + } + + for _, ps := range pss { + client, err := ps.Docker() + if err != nil { + return "", httperr.Server(err) + } + + res, err := client.ListContainers(docker.ListContainersOptions{ + All: true, + Filters: map[string][]string{ + "name": []string{fmt.Sprintf("build-%s", build)}, + }, + }) + + if len(res) > 0 { + return fmt.Sprintf("http://%s:2376", ps.Host), nil + } + } + + time.Sleep(2 * time.Second) + } + + return "", fmt.Errorf("could not find build host") +} + func keepAlive(ws *websocket.Conn, quit chan bool) { c := time.Tick(5 * time.Second) b := []byte{}