Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
fix: attempt to fix regression
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspore committed Oct 12, 2023
1 parent 0a32444 commit 9a1d987
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (t *Tailer) closeFile() {
t.file = nil
}

func (t *Tailer) openFile(ctx context.Context) error {
func (t *Tailer) openFile(ctx context.Context, loc Location) error {
t.closeFile()
for {
var err error
Expand All @@ -179,7 +179,7 @@ func (t *Tailer) openFile(ctx context.Context) error {
}
return errors.Wrap(err, "open")
}
offset, err := t.file.Seek(0, io.SeekCurrent)
offset, err := t.file.Seek(loc.Offset, loc.Whence)
if err != nil {
return errors.Wrap(err, "seek")
}
Expand Down Expand Up @@ -219,8 +219,18 @@ func (t *Tailer) Tail(ctx context.Context, h Handler) error {
}

defer t.closeFile()
if err := t.openFile(ctx); err != nil {
return errors.Wrap(err, "openFile")
{
loc := Location{
Offset: 0,
Whence: io.SeekCurrent,
}
if t.cfg.Location != nil {
loc = *t.cfg.Location
}

if err := t.openFile(ctx, loc); err != nil {
return errors.Wrap(err, "openFile")
}
}

if loc := t.cfg.Location; loc != nil {
Expand Down Expand Up @@ -297,13 +307,13 @@ func (t *Tailer) Tail(ctx context.Context, h Handler) error {
}
return errors.Wrap(err, "wait")
}
default:
return errors.Wrap(readErr, "read")
case nil:
if err := h(ctx, line); err != nil {
return errors.Wrap(err, "handle")
}
line.Data = line.Data[:0] // reset buffer
default:
return errors.Wrap(readErr, "read")
}
}
}
Expand All @@ -330,7 +340,10 @@ func (t *Tailer) waitForChanges(ctx context.Context, pos int64) error {
return errStop
case evTruncated:
t.lg.Info("Re-opening truncated file")
if err := t.openFile(ctx); err != nil {
if err := t.openFile(ctx, Location{
Offset: 0,
Whence: io.SeekStart,
}); err != nil {
return errors.Wrap(err, "open file")
}
t.resetReader()
Expand Down

0 comments on commit 9a1d987

Please sign in to comment.