| 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 swarming | 5 package swarming |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "path" | 9 "path" |
| 10 "sort" | 10 "sort" |
| 11 "strings" | |
| 12 | 11 |
| 13 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
| 14 | 13 |
| 15 mc "github.com/luci/gae/service/memcache" | 14 mc "github.com/luci/gae/service/memcache" |
| 16 "github.com/luci/luci-go/common/api/swarming/swarming/v1" | |
| 17 "github.com/luci/luci-go/common/logging" | 15 "github.com/luci/luci-go/common/logging" |
| 18 ) | 16 ) |
| 19 | 17 |
| 20 // swarmingBuildLogImpl is the implementation for getting a log name from | 18 // swarmingBuildLogImpl is the implementation for getting a log name from |
| 21 // a swarming build via annotee. It returns the full text of the specific log, | 19 // a swarming build via annotee. It returns the full text of the specific log, |
| 22 // and whether or not it has been closed. | 20 // and whether or not it has been closed. |
| 23 func swarmingBuildLogImpl(c context.Context, server, taskID, logname string) (st
ring, bool, error) { | 21 func swarmingBuildLogImpl(c context.Context, svc swarmingService, taskID, lognam
e string) (string, bool, error) { |
| 22 » server := svc.getHost() |
| 24 cached, err := mc.GetKey(c, path.Join("swarmingLog", server, taskID, log
name)) | 23 cached, err := mc.GetKey(c, path.Join("swarmingLog", server, taskID, log
name)) |
| 25 switch { | 24 switch { |
| 26 case err == mc.ErrCacheMiss: | 25 case err == mc.ErrCacheMiss: |
| 27 | 26 |
| 28 case err != nil: | 27 case err != nil: |
| 29 logging.WithError(err).Errorf(c, "failed to fetch log with key %
s from memcache", cached.Key()) | 28 logging.WithError(err).Errorf(c, "failed to fetch log with key %
s from memcache", cached.Key()) |
| 30 | 29 |
| 31 default: | 30 default: |
| 32 logging.Debugf(c, "Cache hit for step log %s/%s/%s", server, tas
kID, logname) | 31 logging.Debugf(c, "Cache hit for step log %s/%s/%s", server, tas
kID, logname) |
| 33 return string(cached.Value()), false, nil | 32 return string(cached.Value()), false, nil |
| 34 } | 33 } |
| 35 | 34 |
| 36 » var sc *swarming.Service | 35 » fetchParams := swarmingFetchParams{ |
| 37 » debug := strings.HasPrefix(taskID, "debug:") | 36 » » fetchRes: true, // Needed so we can validate that this is a Milo
build. |
| 38 » if debug { | 37 » » fetchLog: true, |
| 39 » » // if taskID starts with "debug:", then getTaskOutput will ignor
e client. | |
| 40 » } else { | |
| 41 » » var err error | |
| 42 » » sc, err = getSwarmingClient(c, server) | |
| 43 » » if err != nil { | |
| 44 » » » return "", false, err | |
| 45 » » } | |
| 46 } | 38 } |
| 47 | 39 » fr, err := swarmingFetch(c, svc, taskID, fetchParams) |
| 48 » output, err := getTaskOutput(sc, taskID) | |
| 49 if err != nil { | 40 if err != nil { |
| 50 return "", false, err | 41 return "", false, err |
| 51 } | 42 } |
| 52 | 43 |
| 53 // Decode the data using annotee. | 44 // Decode the data using annotee. |
| 54 » s, err := streamsFromAnnotatedLog(c, output) | 45 » s, err := streamsFromAnnotatedLog(c, fr.log) |
| 55 if err != nil { | 46 if err != nil { |
| 56 return "", false, err | 47 return "", false, err |
| 57 } | 48 } |
| 58 | 49 |
| 59 k := fmt.Sprintf("steps%s", logname) | 50 k := fmt.Sprintf("steps%s", logname) |
| 60 stream, ok := s.Streams[k] | 51 stream, ok := s.Streams[k] |
| 61 if !ok { | 52 if !ok { |
| 62 var keys []string | 53 var keys []string |
| 63 for sk := range s.Streams { | 54 for sk := range s.Streams { |
| 64 keys = append(keys, sk) | 55 keys = append(keys, sk) |
| 65 } | 56 } |
| 66 sort.Strings(keys) | 57 sort.Strings(keys) |
| 67 return "", false, fmt.Errorf("stream %q not found; available str
eams: %q", k, keys) | 58 return "", false, fmt.Errorf("stream %q not found; available str
eams: %q", k, keys) |
| 68 } | 59 } |
| 69 | 60 |
| 70 » if stream.Closed && !debug { | 61 » if stream.Closed { |
| 71 cached.SetValue([]byte(stream.Text)) | 62 cached.SetValue([]byte(stream.Text)) |
| 72 if err := mc.Set(c, cached); err != nil { | 63 if err := mc.Set(c, cached); err != nil { |
| 73 logging.Errorf(c, "Failed to write log with key %s to me
mcache: %s", cached.Key(), err) | 64 logging.Errorf(c, "Failed to write log with key %s to me
mcache: %s", cached.Key(), err) |
| 74 } | 65 } |
| 75 } | 66 } |
| 76 | 67 |
| 77 return stream.Text, stream.Closed, nil | 68 return stream.Text, stream.Closed, nil |
| 78 } | 69 } |
| OLD | NEW |