Skip to content

Commit

Permalink
[chore][fileconsumer] Cleanup file handle when closed by reader (#25912)
Browse files Browse the repository at this point in the history
This PR is a small step towards reducing complexity in the way we manage
readers and their files. With this change we now have an expectation
that reader file handles are always open.
  • Loading branch information
djaglowski authored Aug 21, 2023
1 parent fac35eb commit 48b9fe5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 1 addition & 4 deletions pkg/stanza/fileconsumer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ func (m *Manager) consume(ctx context.Context, paths []string) {
r.ReadToEnd(ctx)
// Delete a file if deleteAfterRead is enabled and we reached the end of the file
if m.deleteAfterRead && r.eof {
r.Close()
if err := os.Remove(r.file.Name()); err != nil {
m.Errorf("could not delete %s", r.file.Name())
}
r.Delete()
}
}(r)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/stanza/fileconsumer/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,25 @@ func (r *reader) finalizeHeader() {
r.HeaderFinalized = true
}

// Delete will close and delete the file
func (r *reader) Delete() {
if r.file == nil {
return
}
f := r.file
r.Close()
if err := os.Remove(f.Name()); err != nil {
r.Errorf("could not delete %s", f.Name())
}
}

// Close will close the file
func (r *reader) Close() {
if r.file != nil {
if err := r.file.Close(); err != nil {
r.Debugw("Problem closing reader", zap.Error(err))
}
r.file = nil
}

if r.headerReader != nil {
Expand Down

0 comments on commit 48b9fe5

Please sign in to comment.