Skip to content
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

Upload with ".tmp" suffix and rename file name after upload #37

Merged
merged 3 commits into from
Nov 7, 2017

Conversation

sakama
Copy link
Contributor

@sakama sakama commented Oct 26, 2017

Current implementation has a problem that part of remote files remains at remote when one of tasks failed.
Some users are checking remote file("/path/to/file.txt") existence with job scheduler or something tools for post processing.

I changed implementation to set temporary_filename and real_filename to Embulk TaskReport at SftpFileOutput.commit().
After all tasks completed, plugin will get each file name sets and rename remote files.
Of course, temporary file will be left in remote server "/path/to/file.txt.tmp".
However, this is help full for simple mechanical check.

I tried to add more test cases #39. However, this doesn't work due to permission problem of uploaded files. I think something "hack" is needed.

@sakama sakama force-pushed the rename-file-after-upload branch 2 times, most recently from d523562 to 715a8f8 Compare October 31, 2017 09:40
@sakama sakama force-pushed the rename-file-after-upload branch from 715a8f8 to ba43aba Compare November 6, 2017 08:14
@sakama sakama mentioned this pull request Nov 6, 2017
@sakama sakama changed the title [WIP] Upload with ".tmp" suffix and rename file name after upload Upload with ".tmp" suffix and rename file name after upload Nov 6, 2017
@sakama
Copy link
Contributor Author

sakama commented Nov 6, 2017

@tvhung83 Could you review this?

@tvhung83
Copy link
Contributor

tvhung83 commented Nov 6, 2017

Sure, taking a look.

@Override
public Void call() throws IOException
{
FileObject remoteFile = newSftpFile(getSftpFileUri(remotePath));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor (optional): we can have multiple resources in one try (...):

try (FileObject remoteFile = newSftpFile(getSftpFileUri(remotePath));
     BufferedOutputStream outputStream = new BufferedOutputStream(remoteFile.getContent().getOutputStream());
     BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(localTempFile))
     ) {
    logger.info("new sftp file: {}", remoteFile.getPublicURIString());
    IOUtils.copy(inputStream, outputStream);
}

It can help to ensure all resources are closed properly :)

private FileObject newSftpFile(final URI sftpUri) throws FileSystemException
{
FileObject file = manager.resolveFile(sftpUri.toString(), fsOptions);
if (file.exists()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: maybe it's nice to have a warn log, that file is going to be overwritten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed at 5e6e74f

file.delete();
}
if (file.getParent().exists()) {
logger.info("parent directory {} exists there", file.getParent().getPublicURIString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto: perhaps a warn log is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think info log is fine because existing parent directory isn't unexpected status.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, this method is about file, not directory creation, so I'm okay with info log here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Copy link
Contributor

@tvhung83 tvhung83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some minor comments (changes are optional)

sftpUtils.uploadFile(tempFile, temporaryFileName);

Map<String, String> executedFiles = new HashMap<>();
executedFiles.put("temporary_filename", fileName + temporaryFileSuffix);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: can use temporaryFileName :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed at e2b2720

@sakama sakama merged commit a5ac5b3 into master Nov 7, 2017
@sakama sakama deleted the rename-file-after-upload branch November 7, 2017 08:24
@sakama sakama restored the rename-file-after-upload branch December 21, 2017 04:35
@sakama sakama deleted the rename-file-after-upload branch December 21, 2017 04:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants