OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package distributor |
| 6 |
| 7 import ( |
| 8 "net/http" |
| 9 "net/url" |
| 10 "strings" |
| 11 |
| 12 "github.com/julienschmidt/httprouter" |
| 13 "github.com/luci/luci-go/appengine/tumble" |
| 14 "github.com/luci/luci-go/common/logging" |
| 15 "golang.org/x/net/context" |
| 16 ) |
| 17 |
| 18 const handlerPattern = "/tq/distributor/:cfgName" |
| 19 |
| 20 func handlerPath(cfgName string) string { |
| 21 return strings.Replace(handlerPattern, ":cfgName", url.QueryEscape(cfgNa
me), 1) |
| 22 } |
| 23 |
| 24 // TaskqueueHandler is the http handler that routes taskqueue tasks made with |
| 25 // Config.EnqueueTask to a distributor's HandleTaskQueueTask method. |
| 26 // |
| 27 // This requires that c already have a Registry installed via the WithRegistry |
| 28 // method. |
| 29 func TaskqueueHandler(c context.Context, rw http.ResponseWriter, r *http.Request
, p httprouter.Params) { |
| 30 defer r.Body.Close() |
| 31 |
| 32 cfgName := p.ByName("cfgName") |
| 33 dist, _, err := GetRegistry(c).MakeDistributor(c, cfgName) |
| 34 if err != nil { |
| 35 logging.Fields{"error": err, "cfg": cfgName}.Errorf(c, "Failed t
o make distributor") |
| 36 http.Error(rw, "bad distributor", http.StatusBadRequest) |
| 37 return |
| 38 } |
| 39 notifications, err := dist.HandleTaskQueueTask(r) |
| 40 if err != nil { |
| 41 logging.Fields{"error": err, "cfg": cfgName}.Errorf(c, "Failed t
o handle taskqueue task") |
| 42 http.Error(rw, "failure to execute handler", http.StatusInternal
ServerError) |
| 43 return |
| 44 } |
| 45 if len(notifications) > 0 { |
| 46 muts := make([]tumble.Mutation, 0, len(notifications)) |
| 47 for _, notify := range notifications { |
| 48 if notify != nil { |
| 49 muts = append(muts, &NotifyExecution{cfgName, no
tify}) |
| 50 } |
| 51 } |
| 52 err = tumble.AddToJournal(c, muts...) |
| 53 if err != nil { |
| 54 logging.Fields{"error": err, "cfg": cfgName}.Errorf(c, "
Failed to handle notifications") |
| 55 http.Error(rw, "failure to handle notifications", http.S
tatusInternalServerError) |
| 56 return |
| 57 } |
| 58 } |
| 59 rw.WriteHeader(http.StatusOK) |
| 60 } |
OLD | NEW |