| OLD | NEW |
| 1 package frontend | 1 package frontend |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "fmt" | 4 "fmt" |
| 5 "io" | 5 "io" |
| 6 "net/http" | 6 "net/http" |
| 7 "time" | 7 "time" |
| 8 | 8 |
| 9 "github.com/luci/gae/service/memcache" | 9 "github.com/luci/gae/service/memcache" |
| 10 "github.com/luci/luci-go/common/logging" | 10 "github.com/luci/luci-go/common/logging" |
| 11 "github.com/luci/luci-go/server/router" | 11 "github.com/luci/luci-go/server/router" |
| 12 | 12 |
| 13 "infra/appengine/test-results/builderstate" | 13 "infra/appengine/test-results/builderstate" |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 // refreshFunc is the function that is called to update cached data | 16 // refreshFunc is the function that is called to update cached data |
| 17 // or on cache miss. | 17 // or on cache miss. |
| 18 // It is global to allow mocking in tests. | 18 // It is global to allow mocking in tests. |
| 19 var refreshFunc = builderstate.RefreshCache | 19 var refreshFunc = builderstate.RefreshCache |
| 20 | 20 |
| 21 // GetBuilderState gets data from the builder state memcache | 21 // getBuilderStateHandler gets data from the builder state memcache |
| 22 // and serves it as JSON. | 22 // and serves it as JSON. |
| 23 func GetBuilderState(ctx *router.Context) { | 23 func getBuilderStateHandler(ctx *router.Context) { |
| 24 c, w := ctx.Context, ctx.Writer | 24 c, w := ctx.Context, ctx.Writer |
| 25 | 25 |
| 26 item, err := memcache.Get(c).Get(builderstate.MemcacheKey) | 26 item, err := memcache.Get(c).Get(builderstate.MemcacheKey) |
| 27 | 27 |
| 28 if err != nil { | 28 if err != nil { |
| 29 item, err = refreshFunc(c) | 29 item, err = refreshFunc(c) |
| 30 if err != nil { | 30 if err != nil { |
| 31 if err == memcache.ErrCacheMiss { | 31 if err == memcache.ErrCacheMiss { |
| 32 err = fmt.Errorf("builderstate: builder data not
generated: %v", err) | 32 err = fmt.Errorf("builderstate: builder data not
generated: %v", err) |
| 33 } | 33 } |
| 34 logging.Errorf(c, err.Error()) | 34 logging.Errorf(c, err.Error()) |
| 35 http.Error(w, err.Error(), http.StatusInternalServerErro
r) | 35 http.Error(w, err.Error(), http.StatusInternalServerErro
r) |
| 36 return | 36 return |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 start := time.Now() | 40 start := time.Now() |
| 41 | 41 |
| 42 w.Header().Set("Content-Type", "application/json") | 42 w.Header().Set("Content-Type", "application/json") |
| 43 w.Header().Set("Access-Control-Allow-Origin", "*") | 43 w.Header().Set("Access-Control-Allow-Origin", "*") |
| 44 n, err := w.Write(item.Value()) | 44 n, err := w.Write(item.Value()) |
| 45 | 45 |
| 46 if err != nil { | 46 if err != nil { |
| 47 logging.Errorf(c, "error writing response: wrote %d bytes of %s,
%s", n, item.Value(), err) | 47 logging.Errorf(c, "error writing response: wrote %d bytes of %s,
%s", n, item.Value(), err) |
| 48 } | 48 } |
| 49 | 49 |
| 50 logging.Debugf(c, "took %s to write response", time.Since(start)) | 50 logging.Debugf(c, "took %s to write response", time.Since(start)) |
| 51 } | 51 } |
| 52 | 52 |
| 53 // UpdateBuilderState refreshes data in the builder state | 53 // updateBuilderStateHandler refreshes data in the builder state |
| 54 // memcache. | 54 // memcache. |
| 55 func UpdateBuilderState(ctx *router.Context) { | 55 func updateBuilderStateHandler(ctx *router.Context) { |
| 56 c, w := ctx.Context, ctx.Writer | 56 c, w := ctx.Context, ctx.Writer |
| 57 _, err := refreshFunc(c) | 57 _, err := refreshFunc(c) |
| 58 | 58 |
| 59 if err != nil { | 59 if err != nil { |
| 60 logging.Errorf(c, err.Error()) | 60 logging.Errorf(c, err.Error()) |
| 61 w.WriteHeader(http.StatusInternalServerError) | 61 w.WriteHeader(http.StatusInternalServerError) |
| 62 io.WriteString(w, err.Error()) | 62 io.WriteString(w, err.Error()) |
| 63 return | 63 return |
| 64 } | 64 } |
| 65 | 65 |
| 66 n, err := io.WriteString(w, "OK") | 66 n, err := io.WriteString(w, "OK") |
| 67 | 67 |
| 68 if err != nil { | 68 if err != nil { |
| 69 logging.Errorf(c, "error writing response: wrote %d bytes of %s,
%s", n, "OK", err) | 69 logging.Errorf(c, "error writing response: wrote %d bytes of %s,
%s", n, "OK", err) |
| 70 } | 70 } |
| 71 } | 71 } |
| OLD | NEW |