Skip to content

Commit

Permalink
start listening for file system events before reading from the logfile
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed Nov 13, 2017
1 parent 48bf800 commit 38c6abe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions tailer/fileTailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func runFileTailer(path string, readall bool, logger simpleLogger, makeWatcher f
writeError(errors, done, "Failed to initialize file system watcher for %v: %v", path, err.Error())
return
}
eventLoop := watcher.StartEventLoop()
defer closeUnlessNil(eventLoop)

// process all pre-existing lines before listening to new events
reader := NewBufferedLineReader()
freshLines, err := reader.ReadAvailableLines(file)
if err != nil {
Expand All @@ -108,9 +112,6 @@ func runFileTailer(path string, readall bool, logger simpleLogger, makeWatcher f
}
}

eventLoop := watcher.StartEventLoop()
defer closeUnlessNil(eventLoop)

for {
select {
case <-done:
Expand Down
4 changes: 2 additions & 2 deletions tailer/fileTailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func testLogrotate(t *testing.T, log simpleLogger, watcherOpt watcherType, logro
// the tailer's errorChannel in case something goes wrong.
go func() {
for err := range tail.Errors() {
t.Fatalf("Tailer failed: %v", err.Error())
t.Errorf("Tailer failed: %v", err.Error()) // Cannot call t.Fatalf() in other goroutine.
}
}()

Expand All @@ -220,7 +220,7 @@ func testLogrotate(t *testing.T, log simpleLogger, watcherOpt watcherType, logro

// Append a line and see if the event is processed.
logFileWriter.writeLine(t, log, "test line 3")
expect(t, log, tail.Lines(), "test line 3", 5*time.Second) // test on Travis CI fails sometimes here although the write event is processed correctly. maybe a timing problem? increase to 5s and see if error persists.
expect(t, log, tail.Lines(), "test line 3", 1*time.Second)

rotate(t, log, logfile, logrotateOpt, logrotateMoveOpt)

Expand Down

0 comments on commit 38c6abe

Please sign in to comment.