forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
348 lines (302 loc) · 10.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Command query-runner runs saved queries and notifies subscribers when the queries have new results.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/inconshreveable/log15"
"github.com/pkg/errors"
"github.com/sourcegraph/sourcegraph/cmd/query-runner/queryrunnerapi"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/debugserver"
"github.com/sourcegraph/sourcegraph/internal/env"
"github.com/sourcegraph/sourcegraph/internal/eventlogger"
"github.com/sourcegraph/sourcegraph/internal/tracer"
)
var forceRunInterval = env.Get("FORCE_RUN_INTERVAL", "", "Force an interval to run saved queries at, instead of assuming query execution time * 30 (query that takes 2s to run, runs every 60s)")
const port = "3183"
func main() {
env.Lock()
env.HandleHelpFlag()
tracer.Init()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGHUP)
<-c
os.Exit(0)
}()
go debugserver.Start()
ctx := context.Background()
if err := api.InternalClient.WaitForFrontend(ctx); err != nil {
log15.Error("failed to wait for frontend", "error", err)
}
http.HandleFunc(queryrunnerapi.PathTestNotification, serveTestNotification)
go func() {
err := executor.run(ctx)
if err != nil {
log15.Error("executor: failed to run due to error", "error", err)
}
}()
host := ""
if env.InsecureDev {
host = "127.0.0.1"
}
addr := net.JoinHostPort(host, port)
log15.Info("query-runner: listening", "addr", addr)
log.Fatalf("Fatal error serving: %s", http.ListenAndServe(addr, nil))
}
func writeError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
err2 := json.NewEncoder(w).Encode(&queryrunnerapi.ErrorResponse{
Message: err.Error(),
})
if err2 != nil {
log15.Error("error encoding HTTP error response", "error", err2.Error(), "writing_error", err.Error())
}
}
// Useful for debugging e.g. email and slack notifications. Set it to true and
// it will send one notification on server startup, effectively.
var debugPretendSavedQueryResultsExist = false
var executor = &executorT{}
type executorT struct {
forceRunInterval *time.Duration
}
func (e *executorT) run(ctx context.Context) error {
// Parse FORCE_RUN_INTERVAL value.
if forceRunInterval != "" {
forceRunInterval, err := time.ParseDuration(forceRunInterval)
if err != nil {
log15.Error("executor: failed to parse FORCE_RUN_INTERVAL", "error", err)
return nil
}
e.forceRunInterval = &forceRunInterval
}
// TODO(slimsag): Make gitserver notify us about repositories being updated
// as we could avoid executing queries if repositories haven't updated
// (impossible for new results to exist).
var oldList map[api.SavedQueryIDSpec]api.ConfigSavedQuery
for {
allSavedQueries, err := api.InternalClient.SavedQueriesListAll(context.Background())
if err != nil {
log15.Error("executor: error fetching saved queries list (trying again in 5s", "error", err)
time.Sleep(5 * time.Second)
continue
}
if oldList != nil {
sendNotificationsForCreatedOrUpdatedOrDeleted(oldList, allSavedQueries)
}
oldList = allSavedQueries
start := time.Now()
for spec, config := range allSavedQueries {
err := e.runQuery(ctx, spec, config)
if err != nil {
log15.Error("executor: failed to run query", "error", err, "query_description", config.Description)
}
}
// If running all the queries didn't take very long (due to them
// erroring out quickly, or if we had zero to run, or if they very
// quickly produced zero results), then sleep for a few second to
// prevent busy waiting and needlessly polling the DB.
if time.Since(start) < time.Second {
time.Sleep(5 * time.Second)
}
}
}
// runQuery runs the given query if an appropriate amount of time has elapsed
// since it last ran.
func (e *executorT) runQuery(ctx context.Context, spec api.SavedQueryIDSpec, query api.ConfigSavedQuery) error {
if !query.Notify && !query.NotifySlack {
// No need to run this query because there will be nobody to notify.
return nil
}
if !strings.Contains(query.Query, "type:diff") && !strings.Contains(query.Query, "type:commit") {
// TODO(slimsag): we temporarily do not support non-commit search
// queries, since those do not support the after:"time" operator.
return nil
}
info, err := api.InternalClient.SavedQueriesGetInfo(ctx, query.Query)
if err != nil {
return errors.Wrap(err, "SavedQueriesGetInfo")
}
// If the saved query was executed recently in the past, then skip it to
// avoid putting too much pressure on searcher/gitserver.
if info != nil {
// We assume a run interval of 30x that which it takes to execute the
// query. For example, a query which takes 2s to execute will run (2s*30)
// every minute.
//
// Additionally, in case queries run very quickly (e.g. our after:
// queries with no results often return in ~15ms), we impose a minimum
// run interval of 10s.
runInterval := info.ExecDuration * 30
if runInterval < 10*time.Second {
runInterval = 10 * time.Second
}
if e.forceRunInterval != nil {
runInterval = *e.forceRunInterval
}
if time.Since(info.LastExecuted) < runInterval {
return nil // too early to run the query
}
}
// Construct a new query which finds search results introduced after the
// last time we queried.
var latestKnownResult time.Time
if info != nil {
latestKnownResult = info.LatestResult
} else {
// We've never executed this search query before, so use the current
// time. We'll most certainly find nothing, which is okay.
latestKnownResult = time.Now()
}
afterTime := latestKnownResult.UTC().Format(time.RFC3339)
newQuery := strings.Join([]string{query.Query, fmt.Sprintf(`after:"%s"`, afterTime)}, " ")
if debugPretendSavedQueryResultsExist {
debugPretendSavedQueryResultsExist = false
newQuery = query.Query
}
// Perform the search and mark the saved query as having been executed in
// the database. We do this regardless of whether or not the search query
// fails in order to avoid e.g. failed saved queries from executing
// constantly and potentially causing harm to the system. We'll retry at
// our normal interval, regardless of errors.
v, execDuration, searchErr := performSearch(ctx, newQuery)
if err := api.InternalClient.SavedQueriesSetInfo(ctx, &api.SavedQueryInfo{
Query: query.Query,
LastExecuted: time.Now(),
LatestResult: latestResultTime(info, v, searchErr),
ExecDuration: execDuration,
}); err != nil {
return errors.Wrap(err, "SavedQueriesSetInfo")
}
if searchErr != nil {
return searchErr
}
// Send notifications for new search results in a separate goroutine, so
// that we don't block other search queries from running in sequence (which
// is done intentionally, to ensure no overloading of searcher/gitserver).
go func() {
if err := notify(context.Background(), spec, query, newQuery, v); err != nil {
log15.Error("executor: failed to send notifications", "error", err)
}
}()
return nil
}
func performSearch(ctx context.Context, query string) (v *gqlSearchResponse, execDuration time.Duration, err error) {
attempts := 0
for {
// Query for search results.
start := time.Now()
v, err := search(ctx, query)
execDuration := time.Since(start)
if err != nil {
return nil, execDuration, errors.Wrap(err, "search")
}
if len(v.Data.Search.Results.Results) > 0 {
return v, execDuration, nil // We have at least some search results, so we're done.
}
cloning := len(v.Data.Search.Results.Cloning)
timedout := len(v.Data.Search.Results.Timedout)
if cloning == 0 && timedout == 0 {
return v, execDuration, nil // zero results, but no cloning or timed out repos. No point in retrying.
}
if attempts > 5 {
return nil, execDuration, fmt.Errorf("found 0 results due to %d cloning %d timedout repos", cloning, timedout)
}
// We didn't find any search results. Some repos are cloning or timed
// out, so try again in a few seconds.
attempts++
log15.Warn("executor: failed to run query found 0 search results due to cloning or timed out repos (retrying in 5s)", "cloning", cloning, "timedout", timedout, "query", query)
time.Sleep(5 * time.Second)
}
}
func latestResultTime(prevInfo *api.SavedQueryInfo, v *gqlSearchResponse, searchErr error) time.Time {
if searchErr != nil || len(v.Data.Search.Results.Results) == 0 {
// Error performing the search, or there were no results. Assume the
// previous info's result time.
if prevInfo != nil {
return prevInfo.LatestResult
}
return time.Now()
}
// Results are ordered chronologically, so first result is the latest.
t, err := extractTime(v.Data.Search.Results.Results[0])
if err != nil {
// Error already logged by extractTime.
return time.Now()
}
return *t
}
var externalURL *url.URL
// notify handles sending notifications for new search results.
func notify(ctx context.Context, spec api.SavedQueryIDSpec, query api.ConfigSavedQuery, newQuery string, results *gqlSearchResponse) error {
if len(results.Data.Search.Results.Results) == 0 {
return nil
}
log15.Info("sending notifications", "new_results", len(results.Data.Search.Results.Results), "description", query.Description)
// Determine which users to notify.
recipients, err := getNotificationRecipients(ctx, spec, query)
if err != nil {
return err
}
// Send slack notifications.
n := ¬ifier{
spec: spec,
query: query,
newQuery: newQuery,
results: results,
recipients: recipients,
}
// Send Slack and email notifications.
n.slackNotify(ctx)
n.emailNotify(ctx)
return nil
}
type notifier struct {
spec api.SavedQueryIDSpec
query api.ConfigSavedQuery
newQuery string
results *gqlSearchResponse
recipients recipients
}
const (
utmSourceEmail = "saved-search-email"
utmSourceSlack = "saved-search-slack"
)
func searchURL(query, utmSource string) string {
if externalURL == nil {
// Determine the external URL.
externalURLStr, err := api.InternalClient.ExternalURL(context.Background())
if err != nil {
log15.Error("failed to get ExternalURL", err)
return ""
}
externalURL, err = url.Parse(externalURLStr)
if err != nil {
log15.Error("failed to parse ExternalURL", err)
return ""
}
}
// Construct URL to the search query.
u := externalURL.ResolveReference(&url.URL{Path: "search"})
q := u.Query()
q.Set("q", query)
q.Set("utm_source", utmSource)
u.RawQuery = q.Encode()
return u.String()
}
func logEvent(userID int32, eventName, eventType string) {
contents, _ := json.Marshal(map[string]string{
"event_type": eventType,
})
eventlogger.LogEvent(userID, eventName, json.RawMessage(contents))
}