diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 14ede77..68e598d 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -42,7 +42,7 @@ jobs: - name: Test # TestFollow fails to work in GitHub Actions. Likely a result of permissions. Will investigate further later - run: go test -race -covermode=atomic -coverprofile=coverage.txt -v -skip TestFollow ./... + run: go test -race -covermode=atomic -coverprofile=coverage.txt -v ./... - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v3 diff --git a/main.go b/main.go index db60a30..fdddfe4 100644 --- a/main.go +++ b/main.go @@ -41,7 +41,7 @@ func main() { logFiles := viper.GetStringSlice("logs") for _, logFile := range logFiles { //begin watching the file in a goroutine for concurrency - go Follow(logFile, pubSub, patterns) + go Follow(logFile, pubSub, patterns, false) } // startup the web server diff --git a/tailer.go b/tailer.go index 2b231d9..b1bbaed 100644 --- a/tailer.go +++ b/tailer.go @@ -5,19 +5,27 @@ import ( "github.com/cskr/pubsub" "github.com/jdrews/go-tailer/fswatcher" "github.com/jdrews/go-tailer/glob" + "time" ) // Follow begins a tailer for the specified logFilePath and publishes log lines to the given pubSub message broker // When Follow picks up a log line, it also runs the line through regex via func Colorize // to determine if it matches a color pattern -func Follow(logFilePath string, pubSub *pubsub.PubSub, patterns []CompiledRegexColors) { +func Follow(logFilePath string, pubSub *pubsub.PubSub, patterns []CompiledRegexColors, polling bool) { + + var tailer fswatcher.FileTailer parsedGlob, err := glob.Parse(logFilePath) if err != nil { panic(fmt.Sprintf("%q: failed to parse glob: %q", parsedGlob, err)) } - tailer, err := fswatcher.RunFileTailer([]glob.Glob{parsedGlob}, false, true, logger) + if polling { + tailer, err = fswatcher.RunPollingFileTailer([]glob.Glob{parsedGlob}, false, true, 10*time.Millisecond, logger) + } else { + tailer, err = fswatcher.RunFileTailer([]glob.Glob{parsedGlob}, false, true, logger) + } + for line := range tailer.Lines() { logger.Debug(line.Line) logMessage := Colorize(line.Line, line.File, patterns) diff --git a/tailer_test.go b/tailer_test.go index 3d914de..3cce6c2 100644 --- a/tailer_test.go +++ b/tailer_test.go @@ -68,7 +68,7 @@ func TestFollow(t *testing.T) { defer pubSub.Unsub(linesChannel, "lines") // Run Follow - go Follow(logFilePath, pubSub, compiledRegexColors) + go Follow(logFilePath, pubSub, compiledRegexColors, true) // Give the fswatcher.RunFileTailer enough time to startup time.Sleep(time.Duration(2000) * time.Millisecond)