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