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

ensure the watcher has started before we read #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ func (tail *Tail) tailFileSync() {

tail.openReader()

if err := tail.watchChanges(); err != nil {
tail.Killf("Error watching for changes on %s: %s", tail.Filename, err)
return
}

var offset int64
var err error

Expand Down Expand Up @@ -331,19 +336,29 @@ func (tail *Tail) tailFileSync() {
}
}

// waitForChanges waits until the file has been appended, deleted,
// moved or truncated. When moved or deleted - the file will be
// reopened if ReOpen is true. Truncated files are always reopened.
func (tail *Tail) waitForChanges() error {
if tail.changes == nil {
// watchChanges ensures the watcher is running.
func (tail *Tail) watchChanges() error {
if tail.changes != nil {
return nil
}
var pos int64
var err error
if !tail.Pipe {
pos, err := tail.file.Seek(0, os.SEEK_CUR)
if err != nil {
return err
}
tail.changes, err = tail.watcher.ChangeEvents(&tail.Tomb, pos)
if err != nil {
return err
}
}
tail.changes, err = tail.watcher.ChangeEvents(&tail.Tomb, pos)
return err
}

// waitForChanges waits until the file has been appended, deleted,
// moved or truncated. When moved or deleted - the file will be
// reopened if ReOpen is true. Truncated files are always reopened.
func (tail *Tail) waitForChanges() error {
if err := tail.watchChanges(); err != nil {
return err
}

select {
Expand Down