handle errors when streaming logs

This commit is contained in:
Alex Dadgar
2017-09-19 07:59:05 -05:00
parent 2e29851b82
commit 46ab2d69b1
4 changed files with 72 additions and 19 deletions

View File

@@ -251,7 +251,12 @@ func (l *LogsCommand) Run(args []string) int {
}
defer r.Close()
io.Copy(os.Stdout, r)
_, err = io.Copy(os.Stdout, r)
if err != nil {
l.Ui.Error(fmt.Sprintf("error following logs: %s", err))
return 1
}
return 0
}
@@ -261,16 +266,18 @@ func (l *LogsCommand) followFile(client *api.Client, alloc *api.Allocation,
follow bool, task, logType, origin string, offset int64) (io.ReadCloser, error) {
cancel := make(chan struct{})
frames, err := client.AllocFS().Logs(alloc, follow, task, logType, origin, offset, cancel, nil)
if err != nil {
frames, errCh := client.AllocFS().Logs(alloc, follow, task, logType, origin, offset, cancel, nil)
select {
case err := <-errCh:
return nil, err
default:
}
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
// Create a reader
var r io.ReadCloser
frameReader := api.NewFrameReader(frames, cancel)
frameReader := api.NewFrameReader(frames, errCh, cancel)
frameReader.SetUnblockTime(500 * time.Millisecond)
r = frameReader