-
Notifications
You must be signed in to change notification settings - Fork 847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(sftp): make sure to delete last file when watch
and delete_on_finish
are enabled
#3037
base: main
Are you sure you want to change the base?
Changes from 1 commit
a36f25e
68eba81
259d12b
18b29aa
ab133f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,11 +174,62 @@ func newSFTPReaderFromParsed(conf *service.ParsedConfig, mgr *service.Resources) | |
} | ||
|
||
func (s *sftpReader) Connect(ctx context.Context) (err error) { | ||
file, nextPath, skip, err := s.seekNextPath(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
if skip { | ||
return nil | ||
} | ||
|
||
details := service.NewScannerSourceDetails() | ||
details.SetName(nextPath) | ||
if s.scanner, err = s.scannerCtor.Create(file, func(ctx context.Context, aErr error) (outErr error) { | ||
_ = s.pathProvider.Ack(ctx, nextPath, aErr) | ||
if aErr != nil { | ||
s.log.Errorf("skipping delete on finish: %s", aErr) | ||
return nil | ||
} | ||
if s.deleteOnFinish { | ||
s.scannerMut.Lock() | ||
client := s.client | ||
if client == nil { | ||
if client, outErr = s.creds.GetClient(s.mgr.FS(), s.address); outErr != nil { | ||
outErr = fmt.Errorf("obtain private client: %w", outErr) | ||
} | ||
defer func() { | ||
_ = client.Close() | ||
}() | ||
} | ||
if outErr == nil { | ||
if outErr = client.Remove(nextPath); outErr != nil { | ||
outErr = fmt.Errorf("remove %v: %w", nextPath, outErr) | ||
} | ||
} | ||
s.scannerMut.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we always return after this block we could defer this right? I just get worried if the unlock is not right after. |
||
} | ||
return | ||
}, details); err != nil { | ||
_ = file.Close() | ||
_ = s.pathProvider.Ack(ctx, nextPath, err) | ||
return err | ||
} | ||
|
||
s.scannerMut.Lock() | ||
s.currentPath = nextPath | ||
s.scannerMut.Unlock() | ||
|
||
s.log.Debugf("Consuming from file '%v'", nextPath) | ||
return | ||
} | ||
|
||
func (s *sftpReader) seekNextPath(ctx context.Context) (file *sftp.File, nextPath string, skip bool, err error) { | ||
s.scannerMut.Lock() | ||
defer s.scannerMut.Unlock() | ||
|
||
if s.scanner != nil { | ||
return nil | ||
skip = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we skip if there is a scanner (and what are we skipping)? Can you add a comment? |
||
return | ||
} | ||
|
||
if s.client == nil { | ||
|
@@ -191,8 +242,6 @@ func (s *sftpReader) Connect(ctx context.Context) (err error) { | |
s.pathProvider = s.getFilePathProvider(ctx) | ||
} | ||
|
||
var nextPath string | ||
var file *sftp.File | ||
for { | ||
if nextPath, err = s.pathProvider.Next(ctx, s.client); err != nil { | ||
if errors.Is(err, sftp.ErrSshFxConnectionLost) { | ||
|
@@ -223,45 +272,9 @@ func (s *sftpReader) Connect(ctx context.Context) (err error) { | |
_ = s.pathProvider.Ack(ctx, nextPath, err) | ||
} | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
details := service.NewScannerSourceDetails() | ||
details.SetName(nextPath) | ||
if s.scanner, err = s.scannerCtor.Create(file, func(ctx context.Context, aErr error) (outErr error) { | ||
_ = s.pathProvider.Ack(ctx, nextPath, aErr) | ||
if aErr != nil { | ||
return nil | ||
} | ||
if s.deleteOnFinish { | ||
s.scannerMut.Lock() | ||
client := s.client | ||
if client == nil { | ||
if client, outErr = s.creds.GetClient(s.mgr.FS(), s.address); outErr != nil { | ||
outErr = fmt.Errorf("obtain private client: %w", outErr) | ||
} | ||
defer func() { | ||
_ = client.Close() | ||
}() | ||
} | ||
if outErr == nil { | ||
if outErr = client.Remove(nextPath); outErr != nil { | ||
outErr = fmt.Errorf("remove %v: %w", nextPath, outErr) | ||
} | ||
} | ||
s.scannerMut.Unlock() | ||
return | ||
} | ||
return | ||
}, details); err != nil { | ||
_ = file.Close() | ||
_ = s.pathProvider.Ack(ctx, nextPath, err) | ||
return err | ||
} | ||
s.currentPath = nextPath | ||
|
||
s.log.Debugf("Consuming from file '%v'", nextPath) | ||
return | ||
} | ||
|
||
func (s *sftpReader) ReadBatch(ctx context.Context) (service.MessageBatch, service.AckFunc, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the assignment of
s.scanner
needs to be under the mutex as well based on theReadBatch
function right?