Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The LUCI Authors. | 1 // Copyright 2015 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 package main | 15 package main |
| 16 | 16 |
| 17 import ( | 17 import ( |
| 18 "context" | |
| 18 "errors" | 19 "errors" |
| 19 "fmt" | 20 "fmt" |
| 20 "log" | 21 "log" |
| 21 "os" | 22 "os" |
| 22 "os/signal" | 23 "os/signal" |
| 23 "path/filepath" | 24 "path/filepath" |
| 24 "time" | 25 "time" |
| 25 | 26 |
| 26 "github.com/golang/protobuf/proto" | 27 "github.com/golang/protobuf/proto" |
| 27 "github.com/maruel/subcommands" | 28 "github.com/maruel/subcommands" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 if err := c.isolateFlags.Parse(cwd, RequireIsolatedFile); err != nil { | 67 if err := c.isolateFlags.Parse(cwd, RequireIsolatedFile); err != nil { |
| 67 return err | 68 return err |
| 68 } | 69 } |
| 69 if len(args) != 0 { | 70 if len(args) != 0 { |
| 70 return errors.New("position arguments not expected") | 71 return errors.New("position arguments not expected") |
| 71 } | 72 } |
| 72 return nil | 73 return nil |
| 73 } | 74 } |
| 74 | 75 |
| 75 func (c *archiveRun) main(a subcommands.Application, args []string) error { | 76 func (c *archiveRun) main(a subcommands.Application, args []string) error { |
| 76 out := os.Stdout | |
| 77 prefix := "\n" | |
| 78 if c.defaultFlags.Quiet { | |
| 79 prefix = "" | |
| 80 } | |
| 81 start := time.Now() | 77 start := time.Now() |
| 82 » client, err := c.createAuthClient() | 78 » authCl, err := c.createAuthClient() |
| 83 if err != nil { | 79 if err != nil { |
| 84 return err | 80 return err |
| 85 } | 81 } |
| 86 ctx := c.defaultFlags.MakeLoggingContext(os.Stderr) | 82 ctx := c.defaultFlags.MakeLoggingContext(os.Stderr) |
| 87 » arch := archiver.New(ctx, isolatedclient.New(nil, client, c.isolatedFlag s.ServerURL, c.isolatedFlags.Namespace, nil, nil), out) | 83 » client := isolatedclient.New(nil, authCl, c.isolatedFlags.ServerURL, c.i solatedFlags.Namespace, nil, nil) |
| 84 | |
| 85 » eventlogger := NewLogger(ctx, c.loggingFlags.EventlogEndpoint) | |
| 86 | |
| 87 » archiveDetails, err := doArchive(ctx, client, &c.ArchiveOptions, c.defau ltFlags.Quiet, start) | |
| 88 » if err != nil { | |
| 89 » » return err | |
| 90 » } | |
| 91 | |
| 92 » end := time.Now() | |
| 93 » op := logpb.IsolateClientEvent_LEGACY_ARCHIVE.Enum() | |
| 94 » if err := eventlogger.logStats(ctx, op, start, end, archiveDetails); err != nil { | |
| 95 » » log.Printf("Failed to log to eventlog: %v", err) | |
| 96 » } | |
| 97 » return nil | |
| 98 } | |
| 99 | |
| 100 // doArchive performs the archive operation for an isolate specified by archiveO pts. | |
| 101 func doArchive(ctx context.Context, client *isolatedclient.Client, archiveOpts * isolate.ArchiveOptions, quiet bool, start time.Time) (*logpb.IsolateClientEvent_ ArchiveDetails, error) { | |
|
mithro
2017/08/02 04:28:34
It seems a bit weird to pass in start time here, b
mcgreevy
2017/08/02 05:16:38
Yeah, I hesitated to do so, but didn't want to cha
| |
| 102 » prefix := "\n" | |
| 103 » if quiet { | |
| 104 » » prefix = "" | |
| 105 » } | |
| 106 | |
| 107 » arch := archiver.New(ctx, client, os.Stdout) | |
| 88 CancelOnCtrlC(arch) | 108 CancelOnCtrlC(arch) |
| 89 » item := isolate.Archive(arch, &c.ArchiveOptions) | 109 » item := isolate.Archive(arch, archiveOpts) |
| 90 item.WaitForHashed() | 110 item.WaitForHashed() |
| 111 var err error | |
| 91 if err = item.Error(); err != nil { | 112 if err = item.Error(); err != nil { |
| 92 » » fmt.Printf("%s%s %s\n", prefix, filepath.Base(c.Isolate), err) | 113 » » fmt.Printf("%s%s %s\n", prefix, filepath.Base(archiveOpts.Isola te), err) |
| 93 } else { | 114 } else { |
| 94 » » fmt.Printf("%s%s %s\n", prefix, item.Digest(), filepath.Base(c. Isolate)) | 115 » » fmt.Printf("%s%s %s\n", prefix, item.Digest(), filepath.Base(ar chiveOpts.Isolate)) |
| 95 } | 116 } |
| 96 if err2 := arch.Close(); err == nil { | 117 if err2 := arch.Close(); err == nil { |
| 97 err = err2 | 118 err = err2 |
| 98 } | 119 } |
| 99 stats := arch.Stats() | 120 stats := arch.Stats() |
| 100 » if !c.defaultFlags.Quiet { | 121 » if !quiet { |
| 101 duration := time.Since(start) | 122 duration := time.Since(start) |
| 102 fmt.Fprintf(os.Stderr, "Hits : %5d (%s)\n", stats.TotalHits() , stats.TotalBytesHits()) | 123 fmt.Fprintf(os.Stderr, "Hits : %5d (%s)\n", stats.TotalHits() , stats.TotalBytesHits()) |
| 103 fmt.Fprintf(os.Stderr, "Misses : %5d (%s)\n", stats.TotalMisses (), stats.TotalBytesPushed()) | 124 fmt.Fprintf(os.Stderr, "Misses : %5d (%s)\n", stats.TotalMisses (), stats.TotalBytesPushed()) |
| 104 fmt.Fprintf(os.Stderr, "Duration: %s\n", units.Round(duration, t ime.Millisecond)) | 125 fmt.Fprintf(os.Stderr, "Duration: %s\n", units.Round(duration, t ime.Millisecond)) |
| 105 } | 126 } |
| 106 | 127 |
| 107 end := time.Now() | |
| 108 | |
| 109 archiveDetails := &logpb.IsolateClientEvent_ArchiveDetails{ | 128 archiveDetails := &logpb.IsolateClientEvent_ArchiveDetails{ |
| 110 HitCount: proto.Int64(int64(stats.TotalHits())), | 129 HitCount: proto.Int64(int64(stats.TotalHits())), |
| 111 MissCount: proto.Int64(int64(stats.TotalMisses())), | 130 MissCount: proto.Int64(int64(stats.TotalMisses())), |
| 112 HitBytes: proto.Int64(int64(stats.TotalBytesHits())), | 131 HitBytes: proto.Int64(int64(stats.TotalBytesHits())), |
| 113 MissBytes: proto.Int64(int64(stats.TotalBytesPushed())), | 132 MissBytes: proto.Int64(int64(stats.TotalBytesPushed())), |
| 114 } | 133 } |
| 115 if item.Error() != nil { | 134 if item.Error() != nil { |
| 116 archiveDetails.IsolateHash = []string{string(item.Digest())} | 135 archiveDetails.IsolateHash = []string{string(item.Digest())} |
| 117 } | 136 } |
| 118 » eventlogger := NewLogger(ctx, c.loggingFlags.EventlogEndpoint) | 137 » return archiveDetails, nil |
| 119 » op := logpb.IsolateClientEvent_LEGACY_ARCHIVE.Enum() | |
| 120 » if err := eventlogger.logStats(ctx, op, start, end, archiveDetails); err != nil { | |
| 121 » » log.Printf("Failed to log to eventlog: %v", err) | |
| 122 » } | |
| 123 » return err | |
| 124 } | 138 } |
| 125 | 139 |
| 126 func (c *archiveRun) Run(a subcommands.Application, args []string, _ subcommands .Env) int { | 140 func (c *archiveRun) Run(a subcommands.Application, args []string, _ subcommands .Env) int { |
| 127 if err := c.Parse(a, args); err != nil { | 141 if err := c.Parse(a, args); err != nil { |
| 128 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) | 142 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) |
| 129 return 1 | 143 return 1 |
| 130 } | 144 } |
| 131 cl, err := c.defaultFlags.StartTracing() | 145 cl, err := c.defaultFlags.StartTracing() |
| 132 if err != nil { | 146 if err != nil { |
| 133 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) | 147 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 149 signal.Notify(interrupted, os.Interrupt) | 163 signal.Notify(interrupted, os.Interrupt) |
| 150 go func() { | 164 go func() { |
| 151 defer signal.Stop(interrupted) | 165 defer signal.Stop(interrupted) |
| 152 select { | 166 select { |
| 153 case <-interrupted: | 167 case <-interrupted: |
| 154 arch.Cancel(errors.New("Ctrl-C")) | 168 arch.Cancel(errors.New("Ctrl-C")) |
| 155 case <-arch.Channel(): | 169 case <-arch.Channel(): |
| 156 } | 170 } |
| 157 }() | 171 }() |
| 158 } | 172 } |
| OLD | NEW |