forked from vmware-archive/atc
/
read_output.go
73 lines (59 loc) · 1.61 KB
/
read_output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package buildserver
import (
"net/http"
"time"
"github.com/concourse/atc"
"github.com/concourse/atc/db"
"code.cloudfoundry.org/lager"
)
func (s *Server) ReadOutputFromBuildPlan(build db.Build) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("read-output", lager.Data{
"build": build.ID(),
})
planID := atc.PlanID(r.FormValue(":plan_id"))
if len(planID) == 0 {
logger.Info("no-plan-id-specified")
w.WriteHeader(http.StatusBadRequest)
return
}
logger.Debug("reading-output", lager.Data{"plan": planID})
cn := w.(http.CloseNotifier).CloseNotify()
for build.Tracker() == "" {
found, err := build.Reload()
if err != nil {
logger.Error("failed-to-reload-build", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Info("build-disappeared")
w.WriteHeader(http.StatusNotFound)
return
}
select {
case <-time.After(time.Second):
case <-cn:
return
}
}
if build.Tracker() == s.peerURL {
engineBuild, err := s.engine.LookupBuild(logger, build)
if err != nil {
logger.Error("failed-to-lookup-build", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
engineBuild.SendOutput(logger, planID, w)
} else {
logger.Debug("forwarding", lager.Data{"to": build.Tracker()})
err := s.forwardRequest(w, r, build.Tracker(), atc.ReadOutputFromBuildPlan)
if err != nil {
logger.Error("failed-to-forward-request", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
})
}