Skip to content

Commit 97ef2b7

Browse files
committed
wait after container exit in logs cmd instead of json_logger for unprocessed data
Signed-off-by: Mrudul Harwani <[email protected]>
1 parent ed7eec1 commit 97ef2b7

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

pkg/cmd/container/logs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/signal"
2424
"syscall"
25+
"time"
2526

2627
"github.com/containerd/containerd"
2728
"github.com/containerd/containerd/errdefs"
@@ -90,6 +91,8 @@ func Logs(ctx context.Context, client *containerd.Client, container string, opti
9091
// Setup goroutine to send stop event if container task finishes:
9192
go func() {
9293
<-waitCh
94+
// wait 100ms to let logViewer process data sent after container exit, if any
95+
time.Sleep(100 * time.Millisecond)
9396
logrus.Debugf("container task has finished, sending kill signal to log viewer")
9497
stopChannel <- os.Interrupt
9598
}()

pkg/logging/json_logger.go

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -157,46 +157,37 @@ func viewLogsJSONFileDirect(lvopts LogViewOptions, jsonLogFilePath string, stdou
157157
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at position %d: %s", jsonLogFilePath, lastPos, err)
158158
}
159159
fin.Close()
160-
161-
readFromLastPos := func() error {
162-
// Re-open the file and seek to the last-consumed offset.
163-
fin, err = os.OpenFile(jsonLogFilePath, os.O_RDONLY, 0400)
164-
if err != nil {
165-
fin.Close()
166-
return fmt.Errorf("error occurred while trying to re-open JSON logfile %q: %s", jsonLogFilePath, err)
167-
}
168-
_, err = fin.Seek(lastPos, 0)
169-
if err != nil {
170-
fin.Close()
171-
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at position %d: %s", jsonLogFilePath, lastPos, err)
172-
}
173-
174-
err = jsonfile.Decode(stdout, stderr, fin, lvopts.Timestamps, lvopts.Since, lvopts.Until, 0)
175-
if err != nil {
176-
fin.Close()
177-
return fmt.Errorf("error occurred while doing follow-up decoding of JSON logfile %q at starting position %d: %s", jsonLogFilePath, lastPos, err)
178-
}
179-
180-
// Record current file seek position before looping again.
181-
lastPos, err = fin.Seek(0, io.SeekCurrent)
182-
if err != nil {
183-
fin.Close()
184-
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at current position: %s", jsonLogFilePath, err)
185-
}
186-
fin.Close()
187-
return nil
188-
}
189-
190160
for {
191161
select {
192162
case <-stopChannel:
193-
logrus.Debugf("received stop signal, re-reading JSON logfile and returning")
194-
// read final logs before returning
195-
return readFromLastPos()
163+
logrus.Debugf("received stop signal while re-reading JSON logfile, returning")
164+
return nil
196165
default:
197-
if err = readFromLastPos(); err != nil {
198-
return err
166+
// Re-open the file and seek to the last-consumed offset.
167+
fin, err = os.OpenFile(jsonLogFilePath, os.O_RDONLY, 0400)
168+
if err != nil {
169+
fin.Close()
170+
return fmt.Errorf("error occurred while trying to re-open JSON logfile %q: %s", jsonLogFilePath, err)
199171
}
172+
_, err = fin.Seek(lastPos, 0)
173+
if err != nil {
174+
fin.Close()
175+
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at position %d: %s", jsonLogFilePath, lastPos, err)
176+
}
177+
178+
err = jsonfile.Decode(stdout, stderr, fin, lvopts.Timestamps, lvopts.Since, lvopts.Until, 0)
179+
if err != nil {
180+
fin.Close()
181+
return fmt.Errorf("error occurred while doing follow-up decoding of JSON logfile %q at starting position %d: %s", jsonLogFilePath, lastPos, err)
182+
}
183+
184+
// Record current file seek position before looping again.
185+
lastPos, err = fin.Seek(0, io.SeekCurrent)
186+
if err != nil {
187+
fin.Close()
188+
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at current position: %s", jsonLogFilePath, err)
189+
}
190+
fin.Close()
200191
}
201192
// Give the OS a second to breathe before re-opening the file:
202193
time.Sleep(time.Second)
@@ -233,8 +224,6 @@ func viewLogsJSONFileThroughTailExec(lvopts LogViewOptions, jsonLogFilePath stri
233224
// Setup killing goroutine:
234225
go func() {
235226
<-stopChannel
236-
// sleep 100ms to get logs post container exit
237-
time.Sleep(100 * time.Millisecond)
238227
logrus.Debugf("killing tail logs process with PID: %d", cmd.Process.Pid)
239228
cmd.Process.Kill()
240229
}()

0 commit comments

Comments
 (0)