Skip to content

Commit

Permalink
add handling for log files that do not exist at the time of adding a …
Browse files Browse the repository at this point in the history
…watch
  • Loading branch information
mcphailtom committed Oct 12, 2023
1 parent 279034d commit 683d233
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func (es ErrorState) String() string {
return "Unknown"
case ErrorStateEndOfStream:
return "End of stream"
case ErrorStateFileNotExist:
return "File does not exist"
case ErrorStateFileRemoved:
return "File removed"
case ErrorStateCancelled:
Expand All @@ -37,6 +39,7 @@ const (
ErrorStateCancelled
ErrorStateFSWatcher
ErrorStateFilePath
ErrorStateFileNotExist
ErrorStateFileRemoved
ErrorStateFileIO
ErrorStateEndOfStream
Expand Down
2 changes: 1 addition & 1 deletion internal/version/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.3.1
v0.4.0
7 changes: 5 additions & 2 deletions logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"
)

// stateEventOp is a type that represents a state event operation.
// Used to communicate state events to the data processor.
type stateEventOp uint8

func (so stateEventOp) String() string {
Expand Down Expand Up @@ -95,12 +97,13 @@ func (lf *logFile) dataProcessor(ctx context.Context, ewCancelChan <-chan error,

if fi != nil && fi.IsDir() {
stopChan <- lf.filePath
lf.errorChan <- NewLogWhaleError(ErrorStateFilePath, fmt.Sprintf("filepath (%s) is a directory, must be a file", lf.filePath), nil)
lf.errorChan <- NewLogWhaleError(ErrorStateFilePath, fmt.Sprintf("filepath (%s) is a directory and must be a file", lf.filePath), nil)
return
}

// If the file doesn't exist, wait for it to be created
if !lf.created {
lf.errorChan <- NewLogWhaleError(ErrorStateFileNotExist, fmt.Sprintf("file does not exist: %s", lf.filePath), nil)
select {
case <-ctx.Done():
return
Expand Down Expand Up @@ -184,7 +187,7 @@ func (lf *logFile) dataProcessor(ctx context.Context, ewCancelChan <-chan error,
return
case so := <-lf.stateEvents:
if so == stateEventRemoved {
lf.errorChan <- NewLogWhaleError(ErrorStateFileRemoved, fmt.Sprintf("file removed, waiting for creation: %s", lf.filePath), nil)
lf.errorChan <- NewLogWhaleError(ErrorStateFileRemoved, fmt.Sprintf("file removed: %s", lf.filePath), nil)
lf.created = false
of.Close()
continue creationLoop
Expand Down
19 changes: 18 additions & 1 deletion logmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,24 @@ func (s *LogManagerTestSuite) TestRemoveLogFile() {
time.Sleep(100 * time.Millisecond)
select {
case err := <-errorChan:
s.Equal(fmt.Sprintf("state: File removed msg: file removed, waiting for creation: %s", of.Name()), err.Error())
s.Equal(fmt.Sprintf("state: File removed msg: file removed: %s", of.Name()), err.Error())
default:
s.Fail("expected error, got none")
}
}

func (s *LogManagerTestSuite) TestFileNotExist() {
fp := os.TempDir() + "doesnotexist"

_, errorChan, err := s.lm.AddLogFile(fp)
s.NoError(err)

time.Sleep(100 * time.Millisecond)
select {
case err := <-errorChan:
s.Equal(fmt.Sprintf("state: File does not exist msg: file does not exist: %s", fp), err.Error())
default:
s.Fail("expected error, got none")
}
}

Expand Down

0 comments on commit 683d233

Please sign in to comment.