Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent a full file read when a file is reopened after truncation #77

Open
ouwe-knutselaar opened this issue Aug 28, 2024 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@ouwe-knutselaar
Copy link

L.S.

I have an issue with Tail. We have an application that watches a file to send changes to an event bus to generate alerts.
The issue is that when the file is truncated and reopened it re-reads the file. The problem is that in our case also all the lines are send to the event bus again. I was digging in to the docs but I could not find a ways to prevent this.

So is there an option in Tail to go to directly to the end of a file without fully reading it when a file is reopened after a truncate?

@ouwe-knutselaar ouwe-knutselaar added the enhancement New feature or request label Aug 28, 2024
@ouwe-knutselaar ouwe-knutselaar changed the title Getting a signal when a file is reopenen Prevent a full file read when a file is reopened after truncation Aug 28, 2024
@KaczDev
Copy link

KaczDev commented Aug 29, 2024

Hey, just had the same issue but I think I managed to resolve it.

Have you tried setting the Location when creating the Tail with config?
This is my conf and it doesn't read the file from the start. I only want to listen for the new logs.

t, err := tail.TailFile(catalinaPath,
	tail.Config{
		Follow:        true,
		CompleteLines: true,
		Location:      &tail.SeekInfo{Offset: 0, Whence: io.SeekEnd},
	})

@ouwe-knutselaar
Copy link
Author

I also found this workaround:

 for line := range tailfile.Lines {
		pos, _ := tailfile.Tell()
		if pos != line.SeekInfo.Offset {
			continue
		}
		sendNewLogLine(line.Text)
	}

It works a little, however the last line in a truncated file is always send this eample.

@ouwe-knutselaar
Copy link
Author

What also Could be done is to add an extra field in the config struct:
ToEndAfterTruncate bool // Go to the end of a file after a truncation to prevent double file read

And then add this piece of code to the function waitForChanges()

	case <-tail.changes.Truncated:
		// Always reopen truncated files (Follow is true)
		tail.Logger.Printf("Re-opening truncated file %s ...", tail.Filename)
		if err := tail.reopen(); err != nil {
			return err
		}
		tail.Logger.Printf("Successfully reopened truncated %s", tail.Filename)
		tail.openReader()
		
		// New snippet
		if tail.ToEndAfterTruncate {
			tail.Logger.Printf("Jump to the end of the file after being truncated %s", tail.Filename)
			if err := tail.seekEnd(); err != nil {
				return err
			}
		}
		// End

		return nil

Test it locally and it works really nice.

@github-staff github-staff deleted a comment from Superstar-IT Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants