This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / spindle / logview / logview.go
1.5 kB 52 lines
1package logview 2 3import ( 4 "bufio" 5 "context" 6 "io" 7 "strings" 8 9 "github.com/hpcloud/tail" 10 "tangled.org/core/spindle/artifactstore" 11 "tangled.org/core/spindle/db" 12 "tangled.org/core/spindle/models" 13) 14 15// streams a workflow's log lines: tails the log file while running, reads 16// the uploaded artifact once finished, same for every role. stop ends a 17// live follow early, the channel closes when the source drains or ctx ends 18func Follow(ctx context.Context, d *db.DB, reader artifactstore.Reader, logDir string, wid models.WorkflowId, finished bool) (<-chan *tail.Line, func(), error) { 19 if finished && reader != nil && d != nil { 20 if fl, err := d.GetFinishedLog(wid.Name); err == nil && fl.Ref != "" { 21 rc, err := reader.Open(ctx, fl.Ref) 22 if err == nil { 23 ch := make(chan *tail.Line, 64) 24 followCtx, cancel := context.WithCancel(ctx) 25 go func() { 26 defer close(ch) 27 defer rc.Close() 28 scanner := bufio.NewScanner(rc) 29 for scanner.Scan() { 30 select { 31 case <-followCtx.Done(): 32 return 33 case ch <- &tail.Line{Text: strings.TrimSuffix(scanner.Text(), "\r")}: 34 } 35 } 36 }() 37 return ch, cancel, nil 38 } 39 } 40 } 41 42 t, err := tail.TailFile(models.LogFilePath(logDir, wid), tail.Config{ 43 Follow: !finished, 44 ReOpen: !finished, 45 MustExist: false, 46 Location: &tail.SeekInfo{Offset: 0, Whence: io.SeekStart}, 47 }) 48 if err != nil { 49 return nil, nil, err 50 } 51 return t.Lines, func() { _ = t.Stop() }, nil 52}