| 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 buildbot | 5 package buildbot |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "compress/gzip" | 9 "compress/gzip" |
| 10 "compress/zlib" | 10 "compress/zlib" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // Extract OS and OS Family | 173 // Extract OS and OS Family |
| 174 if v, ok := hostInfo["os family"]; ok { | 174 if v, ok := hostInfo["os family"]; ok { |
| 175 family = v | 175 family = v |
| 176 } | 176 } |
| 177 if v, ok := hostInfo["os version"]; ok { | 177 if v, ok := hostInfo["os version"]; ok { |
| 178 version = v | 178 version = v |
| 179 } | 179 } |
| 180 return | 180 return |
| 181 } | 181 } |
| 182 | 182 |
| 183 // Marks a build as finished and expired. |
| 184 func expireBuild(c context.Context, b *buildbotBuild) error { |
| 185 finished := float64(clock.Now(c).Unix()) |
| 186 if b.TimeStamp != nil { |
| 187 finished = float64(*b.TimeStamp) |
| 188 } |
| 189 results := int(2) |
| 190 b.Times[1] = &finished |
| 191 b.Finished = true |
| 192 b.Results = &results |
| 193 return ds.Put(c, b) |
| 194 } |
| 195 |
| 183 // PubSubHandler is a webhook that stores the builds coming in from pubsub. | 196 // PubSubHandler is a webhook that stores the builds coming in from pubsub. |
| 184 func PubSubHandler(ctx *router.Context) { | 197 func PubSubHandler(ctx *router.Context) { |
| 185 c, h, r := ctx.Context, ctx.Writer, ctx.Request | 198 c, h, r := ctx.Context, ctx.Writer, ctx.Request |
| 186 | 199 |
| 187 msg := pubSubSubscription{} | 200 msg := pubSubSubscription{} |
| 188 defer r.Body.Close() | 201 defer r.Body.Close() |
| 189 dec := json.NewDecoder(r.Body) | 202 dec := json.NewDecoder(r.Body) |
| 190 if err := dec.Decode(&msg); err != nil { | 203 if err := dec.Decode(&msg); err != nil { |
| 191 logging.WithError(err).Errorf( | 204 logging.WithError(err).Errorf( |
| 192 c, "Could not decode message. %s", err) | 205 c, "Could not decode message. %s", err) |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 if buildExists { | 289 if buildExists { |
| 277 buildCounter.Add( | 290 buildCounter.Add( |
| 278 c, 1, false, build.Master, build.Buildername, bu
ild.Finished, "Replaced") | 291 c, 1, false, build.Master, build.Buildername, bu
ild.Finished, "Replaced") |
| 279 } else { | 292 } else { |
| 280 buildCounter.Add( | 293 buildCounter.Add( |
| 281 c, 1, false, build.Master, build.Buildername, bu
ild.Finished, "New") | 294 c, 1, false, build.Master, build.Buildername, bu
ild.Finished, "New") |
| 282 } | 295 } |
| 283 | 296 |
| 284 } | 297 } |
| 285 if master != nil { | 298 if master != nil { |
| 299 // Store the master json into the datastore. |
| 286 err = putDSMasterJSON(c, master, internal) | 300 err = putDSMasterJSON(c, master, internal) |
| 287 if err != nil { | 301 if err != nil { |
| 288 logging.WithError(err).Errorf( | 302 logging.WithError(err).Errorf( |
| 289 c, "Could not save master in datastore %s", err) | 303 c, "Could not save master in datastore %s", err) |
| 290 // This is transient, we do want PubSub to retry. | 304 // This is transient, we do want PubSub to retry. |
| 291 h.WriteHeader(500) | 305 h.WriteHeader(500) |
| 292 return | 306 return |
| 293 } | 307 } |
| 308 |
| 309 // Extract current builds data out of the master json, and use i
t to |
| 310 // clean up expired builds. |
| 311 q := ds.NewQuery("buildbotBuild"). |
| 312 Eq("finished", false). |
| 313 Eq("master", master.Name) |
| 314 builds := []*buildbotBuild{} |
| 315 err := ds.GetAll(c, q, &builds) |
| 316 if err != nil { |
| 317 logging.WithError(err).Errorf(c, "Could not load current
builds from master %s", |
| 318 master.Name) |
| 319 h.WriteHeader(500) |
| 320 return |
| 321 } |
| 322 logging.Infof(c, "======Checking current builds %s, got %d======
", q, len(builds)) |
| 323 for _, b := range builds { |
| 324 builder, ok := master.Builders[b.Buildername] |
| 325 if !ok { |
| 326 // Mark this build due to builder being removed. |
| 327 logging.Infof(c, "Expiring %s/%s/%d due to build
er being removed", |
| 328 master.Name, b.Buildername, b.Number) |
| 329 err = expireBuild(c, b) |
| 330 if err != nil { |
| 331 logging.WithError(err).Errorf(c, "Could
not expire build") |
| 332 h.WriteHeader(500) |
| 333 return |
| 334 } |
| 335 } |
| 336 |
| 337 found := false |
| 338 for _, bnum := range builder.CurrentBuilds { |
| 339 if b.Number == bnum { |
| 340 found = true |
| 341 break |
| 342 } |
| 343 } |
| 344 if !found { |
| 345 // Mark this build due to build not current anym
ore. |
| 346 logging.Infof(c, "Expiring %s/%s/%d due to build
not current", |
| 347 master.Name, b.Buildername, b.Number) |
| 348 err = expireBuild(c, b) |
| 349 if err != nil { |
| 350 logging.WithError(err).Errorf(c, "Could
not expire build") |
| 351 h.WriteHeader(500) |
| 352 return |
| 353 } |
| 354 } |
| 355 } |
| 294 } | 356 } |
| 295 h.WriteHeader(200) | 357 h.WriteHeader(200) |
| 296 } | 358 } |
| OLD | NEW |