Skip to content

Commit

Permalink
try a few times to find the build host
Browse files Browse the repository at this point in the history
  • Loading branch information
ddollar committed Aug 1, 2016
1 parent edd9cfc commit e3251a2
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions api/controllers/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit e3251a2

Please sign in to comment.