-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (91 loc) · 3.29 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2017 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"net/http"
"time"
"google.golang.org/appengine"
authServer "go.chromium.org/luci/appengine/gaeauth/server"
"go.chromium.org/luci/appengine/gaemiddleware"
"go.chromium.org/luci/appengine/gaemiddleware/standard"
"go.chromium.org/luci/appengine/tq"
"go.chromium.org/luci/common/errors"
"go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/retry/transient"
"go.chromium.org/luci/common/tsmon/field"
"go.chromium.org/luci/common/tsmon/metric"
"go.chromium.org/luci/config/appengine/gaeconfig"
"go.chromium.org/luci/config/impl/remote"
"go.chromium.org/luci/server/auth"
"go.chromium.org/luci/server/router"
"go.chromium.org/luci/luci_notify/config"
"go.chromium.org/luci/luci_notify/notify"
)
var bulidbucketPubSub = metric.NewCounter(
"luci/notify/buildbucket-pubsub",
"Number of received Buildbucket PubSub messages",
nil,
// "success", "transient-failure" or "permanent-failure"
field.String("status"),
)
func main() {
r := router.New()
standard.InstallHandlers(r)
basemw := standard.Base().Extend(auth.Authenticate(authServer.CookieAuth), withRemoteConfigService)
taskDispatcher := tq.Dispatcher{BaseURL: "/internal/tasks/"}
notify.InitDispatcher(&taskDispatcher)
taskDispatcher.InstallRoutes(r, basemw)
// Cron endpoint.
r.GET("/internal/cron/update-config", basemw.Extend(gaemiddleware.RequireCron), config.UpdateHandler)
// Pub/Sub endpoint.
r.POST("/_ah/push-handlers/buildbucket", basemw, func(c *router.Context) {
c.Context, _ = context.WithTimeout(c.Context, 50*time.Second)
ctx := c.Context
status := ""
switch err := notify.BuildbucketPubSubHandler(c, &taskDispatcher); {
case transient.Tag.In(err) || appengine.IsTimeoutError(errors.Unwrap(err)):
status = "transient-failure"
logging.Errorf(ctx, "transient failure: %s", err)
// Retry the message.
c.Writer.WriteHeader(http.StatusInternalServerError)
case err != nil:
status = "permanent-failure"
logging.Errorf(ctx, "permanent failure: %s", err)
default:
status = "success"
}
bulidbucketPubSub.Add(ctx, 1, status)
})
http.Handle("/", r)
appengine.Main()
}
func withRemoteConfigService(c *router.Context, next router.Handler) {
s, err := gaeconfig.FetchCachedSettings(c.Context)
if err != nil {
c.Writer.WriteHeader(http.StatusInternalServerError)
logging.WithError(err).Errorf(c.Context, "failure retrieving cached settings")
return
}
rInterface := remote.New(s.ConfigServiceHost, false, func(c context.Context) (*http.Client, error) {
t, err := auth.GetRPCTransport(c, auth.AsSelf)
if err != nil {
return nil, err
}
return &http.Client{Transport: t}, nil
})
// insert into context
c.Context = config.WithConfigService(c.Context, rInterface)
next(c)
}