18790: Prefix each line with source filename.
authorTom Clegg <tom@curii.com>
Tue, 4 Apr 2023 14:59:41 +0000 (10:59 -0400)
committerTom Clegg <tom@curii.com>
Tue, 4 Apr 2023 16:52:59 +0000 (12:52 -0400)
Re-check container state right away if crunch-run.txt logs a final
state like "Completed".

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

cmd/arvados-client/container_gateway.go
cmd/arvados-client/container_gateway_test.go

index 7a35190427c650e6df8bad90ec83f837e6d23130..f23252169f6397ccd58b66dab1fd96bdfee6c776 100644 (file)
@@ -111,17 +111,18 @@ poll:
                                continue poll
                        }
                }
-               anyNewData := false
+               newData := map[string]*bytes.Buffer{}
                for _, fnm := range watching {
                        if size[fnm] > mark[fnm] {
-                               anyNewData = true
-                               _, n, err := lc.copyRange(ctx, ctrUUID, fnm, fmt.Sprintf("%d-", mark[fnm]), stdout)
+                               newData[fnm] = &bytes.Buffer{}
+                               _, n, err := lc.copyRange(ctx, ctrUUID, fnm, fmt.Sprintf("%d-", mark[fnm]), newData[fnm])
                                if err != nil {
                                        fmt.Fprintln(stderr, err)
                                }
                                mark[fnm] += n
                        }
                }
+               checkState := lc.display(stdout, stderr, watching, newData)
                if containerFinished {
                        // If the caller specified a container request
                        // UUID and the container we were watching has
@@ -143,9 +144,12 @@ poll:
                        ctrUUID = newUUID
                        containerFinished = false
                        delay = 0
-               } else if anyNewData {
+                       continue
+               }
+               if len(newData) > 0 {
                        delay = pollInterval
-               } else {
+               }
+               if len(newData) == 0 || checkState {
                        delay = delay * 2
                        if delay > pollInterval*5 {
                                delay = pollInterval * 5
@@ -210,6 +214,33 @@ func (lc *logsCommand) copyRange(ctx context.Context, uuid, fnm, byterange strin
        return rsize, n, err
 }
 
+// display some log data, formatted as desired (prefixing each line
+// with a tag indicating which file it came from, etc.).
+//
+// Return value is true if the log data contained a hint that it's a
+// good time to check whether the container is finished so we can
+// exit.
+func (lc *logsCommand) display(out, stderr io.Writer, watching []string, received map[string]*bytes.Buffer) bool {
+       checkState := false
+       for _, fnm := range watching {
+               buf := received[fnm]
+               if buf == nil || buf.Len() == 0 {
+                       continue
+               }
+               for _, line := range bytes.Split(bytes.TrimSuffix(buf.Bytes(), []byte{'\n'}), []byte{'\n'}) {
+                       _, err := fmt.Fprintf(out, "%-14s %s\n", fnm, line)
+                       if err != nil {
+                               fmt.Fprintln(stderr, err)
+                       }
+                       checkState = checkState ||
+                               bytes.HasSuffix(line, []byte("Complete")) ||
+                               bytes.HasSuffix(line, []byte("Cancelled")) ||
+                               bytes.HasSuffix(line, []byte("Queued"))
+               }
+       }
+       return checkState
+}
+
 // shellCommand connects the terminal to an interactive shell on a
 // running container.
 type shellCommand struct{}
index 0e5aad709ef766be416c0217a64e13a03ee92f03..fbab4990027aa3b429ff2db7f5d31111ddc1b01c 100644 (file)
@@ -277,7 +277,7 @@ func (s *ClientSuite) TestContainerLog(c *check.C) {
 
        for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline) && !strings.Contains(stdout.String(), "--end--"); time.Sleep(time.Second / 10) {
        }
-       c.Check(stdout.String(), check.Matches, `(?ms).*--end--\n.*`)
+       c.Check(stdout.String(), check.Matches, `(?ms).*stderr\.txt +--end--\n.*`)
 
        mtxt, err := cfs.MarshalManifest(".")
        c.Assert(err, check.IsNil)