Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
Go
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374package logview
import ( "bufio" "context" "errors" "fmt" "io" "os" "strings"
"github.com/hpcloud/tail" "tangled.org/core/spindle/artifactstore" "tangled.org/core/spindle/db" "tangled.org/core/spindle/models")
// streams a workflow's log lines: tails the log file while running, reads// the uploaded artifact once finished, same for every role. stop ends a// live follow early, the channel closes when the source drains or ctx endsfunc Follow(ctx context.Context, d *db.DB, reader artifactstore.Reader, logDir string, wid models.WorkflowId, finished bool) (<-chan *tail.Line, func(), error) { logPath := models.LogFilePath(logDir, wid) var artifactErr error if finished && reader != nil && d != nil { fl, err := d.GetFinishedLog(wid) if err != nil { artifactErr = fmt.Errorf("find archived workflow log: %w", err) } else if fl.Ref == "" { artifactErr = errors.New("archived workflow log has an empty ref") } else { rc, err := reader.Open(ctx, fl.Ref) if err != nil { artifactErr = fmt.Errorf("open archived workflow log: %w", err) } else { ch := make(chan *tail.Line, 64) followCtx, cancel := context.WithCancel(ctx) go func() { defer close(ch) defer rc.Close() scanner := bufio.NewScanner(rc) for scanner.Scan() { select { case <-followCtx.Done(): return case ch <- &tail.Line{Text: strings.TrimSuffix(scanner.Text(), "\r")}: } } }() return ch, cancel, nil } } }
if artifactErr != nil { if _, err := os.Stat(logPath); err != nil { if errors.Is(err, os.ErrNotExist) { return nil, nil, fmt.Errorf("finished workflow log unavailable: %w", artifactErr) } return nil, nil, fmt.Errorf("stat local workflow log: %w", err) } }
t, err := tail.TailFile(logPath, tail.Config{ Follow: !finished, ReOpen: !finished, MustExist: false, Location: &tail.SeekInfo{Offset: 0, Whence: io.SeekStart}, }) if err != nil { return nil, nil, err } return t.Lines, func() { _ = t.Stop() }, nil}