From 78d9f210b9aa629b6a9f12cfe79c7fc56ae50238 Mon Sep 17 00:00:00 2001 From: Jon Drews Date: Fri, 16 Jun 2023 20:22:36 -0400 Subject: [PATCH 1/2] Enable TestFollow --- .github/workflows/build-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 443e722907723d27fbd85abe54aff6635dc7279e Mon Sep 17 00:00:00 2001 From: Jon Drews Date: Fri, 16 Jun 2023 20:37:44 -0400 Subject: [PATCH 2/2] Use PollingFileTailer for tests --- main.go | 2 +- tailer.go | 12 ++++++++++-- tailer_test.go | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) 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)