| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package tsmon | 5 package tsmon |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 | 9 |
| 10 "github.com/julienschmidt/httprouter" | |
| 11 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
| 12 | 11 |
| 13 "github.com/luci/gae/service/datastore" | 12 "github.com/luci/gae/service/datastore" |
| 14 "github.com/luci/gae/service/info" | 13 "github.com/luci/gae/service/info" |
| 15 "github.com/luci/luci-go/common/clock" | 14 "github.com/luci/luci-go/common/clock" |
| 16 "github.com/luci/luci-go/common/logging" | 15 "github.com/luci/luci-go/common/logging" |
| 17 "github.com/luci/luci-go/common/parallel" | 16 "github.com/luci/luci-go/common/parallel" |
| 18 "github.com/luci/luci-go/common/tsmon" | 17 "github.com/luci/luci-go/common/tsmon" |
| 19 » "github.com/luci/luci-go/server/middleware" | 18 » "github.com/luci/luci-go/server/router" |
| 20 ) | 19 ) |
| 21 | 20 |
| 22 // InstallHandlers installs HTTP handlers for tsmon routes. | 21 // InstallHandlers installs HTTP handlers for tsmon routes. |
| 23 func InstallHandlers(r *httprouter.Router, base middleware.Base) { | 22 func InstallHandlers(r *router.Router, base router.MiddlewareChain) { |
| 24 » r.GET("/internal/cron/ts_mon/housekeeping", base(housekeepingHandler)) | 23 » r.GET("/internal/cron/ts_mon/housekeeping", base, housekeepingHandler) |
| 25 } | 24 } |
| 26 | 25 |
| 27 // housekeepingHandler is an HTTP handler that should be run every minute by | 26 // housekeepingHandler is an HTTP handler that should be run every minute by |
| 28 // cron on App Engine. It assigns task numbers to datastore entries, and runs | 27 // cron on App Engine. It assigns task numbers to datastore entries, and runs |
| 29 // any global metric callbacks. | 28 // any global metric callbacks. |
| 30 func housekeepingHandler(c context.Context, rw http.ResponseWriter, r *http.Requ
est, p httprouter.Params) { | 29 func housekeepingHandler(c *router.Context) { |
| 31 » if !info.Get(c).IsDevAppServer() && r.Header.Get("X-Appengine-Cron") !=
"true" { | 30 » if !info.Get(c.Context).IsDevAppServer() && c.Request.Header.Get("X-Appe
ngine-Cron") != "true" { |
| 32 » » rw.WriteHeader(http.StatusForbidden) | 31 » » c.Writer.WriteHeader(http.StatusForbidden) |
| 33 » » http.Error(rw, "request not made from cron", http.StatusForbidde
n) | 32 » » http.Error(c.Writer, "request not made from cron", http.StatusFo
rbidden) |
| 34 return | 33 return |
| 35 } | 34 } |
| 36 | 35 |
| 37 » if err := assignTaskNumbers(c); err != nil { | 36 » if err := assignTaskNumbers(c.Context); err != nil { |
| 38 » » rw.WriteHeader(http.StatusInternalServerError) | 37 » » c.Writer.WriteHeader(http.StatusInternalServerError) |
| 39 } | 38 } |
| 40 | 39 |
| 41 » tsmon.GetState(c).RunGlobalCallbacks(c) | 40 » tsmon.GetState(c.Context).RunGlobalCallbacks(c.Context) |
| 42 } | 41 } |
| 43 | 42 |
| 44 // assignTaskNumbers does some housekeeping on the datastore entries for App | 43 // assignTaskNumbers does some housekeeping on the datastore entries for App |
| 45 // Engine instances - assigning unique task numbers to those without ones set, | 44 // Engine instances - assigning unique task numbers to those without ones set, |
| 46 // and expiring old entities. | 45 // and expiring old entities. |
| 47 func assignTaskNumbers(c context.Context) error { | 46 func assignTaskNumbers(c context.Context) error { |
| 48 c = info.Get(c).MustNamespace(instanceNamespace) | 47 c = info.Get(c).MustNamespace(instanceNamespace) |
| 49 ds := datastore.Get(c) | 48 ds := datastore.Get(c) |
| 50 | 49 |
| 51 logger := logging.Get(c) | 50 logger := logging.Get(c) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 for { | 103 for { |
| 105 n := next | 104 n := next |
| 106 next++ | 105 next++ |
| 107 _, has := used[n] | 106 _, has := used[n] |
| 108 if !has { | 107 if !has { |
| 109 return n | 108 return n |
| 110 } | 109 } |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 } | 112 } |
| OLD | NEW |