Skip to content

Commit

Permalink
Checking for creation of logfile, without CPU spin, but bail if too m…
Browse files Browse the repository at this point in the history
…any errors
  • Loading branch information
sychan committed May 7, 2018
1 parent 7c5cd7c commit 28ad938
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"time"

"github.com/hpcloud/tail"
"golang.org/x/net/context"
Expand All @@ -12,17 +13,24 @@ import (
func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
defer wg.Done()

var isPipe bool
var errCount int

s, err := os.Stat(file)
if err != nil {
log.Fatalf("unable to stat %s: %s", file, err)
log.Printf("Warning: unable to stat %s: %s", file, err)
errCount++
isPipe = false
} else {
isPipe = s.Mode()&os.ModeNamedPipe != 0
}

t, err := tail.TailFile(file, tail.Config{
Follow: true,
ReOpen: true,
Poll: poll,
Logger: tail.DiscardingLogger,
Pipe: s.Mode()&os.ModeNamedPipe != 0,
Pipe: isPipe,
})
if err != nil {
log.Fatalf("unable to tail %s: %s", file, err)
Expand All @@ -41,15 +49,19 @@ func tailFile(ctx context.Context, file string, poll bool, dest *os.File) {
return
// get the next log line and echo it out
case line := <-t.Lines:
if line == nil {
if t.Err() != nil {
log.Fatalf("unable to tail %s: %s", file, t.Err())
if t.Err() != nil {
log.Printf("Warning: unable to tail %s: %s", file, t.Err())
errCount++
if errCount > 100 {
log.Fatalf("Logged %d consecutive errors while tailing. Exiting", errCount)
}
time.Sleep(2 * time.Second) // Sleep for 2 seconds before retrying
} else if line == nil {
return
} else if line.Err != nil {
log.Fatalf("unable to tail %s: %s", file, t.Err())
} else {
fmt.Fprintln(dest, line.Text)
errCount = 0 // Zero the error count
}
fmt.Fprintln(dest, line.Text)
}
}
}

0 comments on commit 28ad938

Please sign in to comment.