OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 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 buildbucket implements tasks that run Buildbucket jobs. | 5 // Package buildbucket implements tasks that run Buildbucket jobs. |
6 package buildbucket | 6 package buildbucket |
7 | 7 |
8 import ( | 8 import ( |
9 "encoding/json" | 9 "encoding/json" |
10 "fmt" | 10 "fmt" |
11 "net/url" | 11 "net/url" |
12 "time" | 12 "time" |
13 | 13 |
14 "github.com/golang/protobuf/proto" | 14 "github.com/golang/protobuf/proto" |
15 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
16 "google.golang.org/api/pubsub/v1" | 16 "google.golang.org/api/pubsub/v1" |
17 | 17 |
18 "github.com/luci/gae/service/info" | 18 "github.com/luci/gae/service/info" |
19 "github.com/luci/luci-go/common/api/buildbucket/buildbucket/v1" | 19 "github.com/luci/luci-go/common/api/buildbucket/buildbucket/v1" |
20 "github.com/luci/luci-go/common/errors" | 20 "github.com/luci/luci-go/common/errors" |
| 21 "github.com/luci/luci-go/common/retry/transient" |
21 "github.com/luci/luci-go/scheduler/appengine/messages" | 22 "github.com/luci/luci-go/scheduler/appengine/messages" |
22 "github.com/luci/luci-go/scheduler/appengine/task" | 23 "github.com/luci/luci-go/scheduler/appengine/task" |
23 "github.com/luci/luci-go/scheduler/appengine/task/utils" | 24 "github.com/luci/luci-go/scheduler/appengine/task/utils" |
24 ) | 25 ) |
25 | 26 |
26 const ( | 27 const ( |
27 statusCheckTimerName = "check-buildbucket-build-status" | 28 statusCheckTimerName = "check-buildbucket-build-status" |
28 statusCheckTimerInterval = time.Minute | 29 statusCheckTimerInterval = time.Minute |
29 ) | 30 ) |
30 | 31 |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 } | 290 } |
290 } | 291 } |
291 | 292 |
292 func (m TaskManager) checkBuildStatus(c context.Context, ctl task.Controller) er
ror { | 293 func (m TaskManager) checkBuildStatus(c context.Context, ctl task.Controller) er
ror { |
293 switch status := ctl.State().Status; { | 294 switch status := ctl.State().Status; { |
294 // This can happen if Buildbucket manages to send PubSub message before | 295 // This can happen if Buildbucket manages to send PubSub message before |
295 // LaunchTask finishes. Do not touch State or DebugLog to avoid collisio
n with | 296 // LaunchTask finishes. Do not touch State or DebugLog to avoid collisio
n with |
296 // still running LaunchTask when saving the invocation, it will only mak
e the | 297 // still running LaunchTask when saving the invocation, it will only mak
e the |
297 // matters worse. | 298 // matters worse. |
298 case status == task.StatusStarting: | 299 case status == task.StatusStarting: |
299 » » return errors.WrapTransient(errors.New("invocation is still star
ting, try again later")) | 300 » » return errors.New("invocation is still starting, try again later
", transient.Tag) |
300 case status != task.StatusRunning: | 301 case status != task.StatusRunning: |
301 return fmt.Errorf("unexpected invocation status %q, expecting %q
", status, task.StatusRunning) | 302 return fmt.Errorf("unexpected invocation status %q, expecting %q
", status, task.StatusRunning) |
302 } | 303 } |
303 | 304 |
304 // Grab build ID from the blob generated in LaunchTask. | 305 // Grab build ID from the blob generated in LaunchTask. |
305 taskData := taskData{} | 306 taskData := taskData{} |
306 if err := json.Unmarshal(ctl.State().TaskData, &taskData); err != nil { | 307 if err := json.Unmarshal(ctl.State().TaskData, &taskData); err != nil { |
307 ctl.State().Status = task.StatusFailed | 308 ctl.State().Status = task.StatusFailed |
308 return fmt.Errorf("could not parse TaskData - %s", err) | 309 return fmt.Errorf("could not parse TaskData - %s", err) |
309 } | 310 } |
310 | 311 |
311 // Fetch build result from Buildbucket. | 312 // Fetch build result from Buildbucket. |
312 service, err := m.createBuildbucketService(c, ctl) | 313 service, err := m.createBuildbucketService(c, ctl) |
313 if err != nil { | 314 if err != nil { |
314 return err | 315 return err |
315 } | 316 } |
316 resp, err := service.Get(taskData.BuildID).Do() | 317 resp, err := service.Get(taskData.BuildID).Do() |
317 if err != nil { | 318 if err != nil { |
318 ctl.DebugLog("Failed to fetch buildbucket build - %s", err) | 319 ctl.DebugLog("Failed to fetch buildbucket build - %s", err) |
319 err = utils.WrapAPIError(err) | 320 err = utils.WrapAPIError(err) |
320 » » if !errors.IsTransient(err) { | 321 » » if !transient.Tag.In(err) { |
321 ctl.State().Status = task.StatusFailed | 322 ctl.State().Status = task.StatusFailed |
322 } | 323 } |
323 return err | 324 return err |
324 } | 325 } |
325 | 326 |
326 // Dump response in full to the debug log. It doesn't contain any secret
s. | 327 // Dump response in full to the debug log. It doesn't contain any secret
s. |
327 blob, err := json.MarshalIndent(resp, "", " ") | 328 blob, err := json.MarshalIndent(resp, "", " ") |
328 if err != nil { | 329 if err != nil { |
329 return err | 330 return err |
330 } | 331 } |
(...skipping 28 matching lines...) Expand all Loading... |
359 r.Id, r.Status, r.Result, r.FailureReason, r.CancelationReason) | 360 r.Id, r.Status, r.Result, r.FailureReason, r.CancelationReason) |
360 switch { | 361 switch { |
361 case r.Status == "SCHEDULED" || r.Status == "STARTED": | 362 case r.Status == "SCHEDULED" || r.Status == "STARTED": |
362 return // do nothing | 363 return // do nothing |
363 case r.Status == "COMPLETED" && r.Result == "SUCCESS": | 364 case r.Status == "COMPLETED" && r.Result == "SUCCESS": |
364 ctl.State().Status = task.StatusSucceeded | 365 ctl.State().Status = task.StatusSucceeded |
365 default: | 366 default: |
366 ctl.State().Status = task.StatusFailed | 367 ctl.State().Status = task.StatusFailed |
367 } | 368 } |
368 } | 369 } |
OLD | NEW |