-
Notifications
You must be signed in to change notification settings - Fork 4
Added testsets and an upload function that can do retries #42
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
Changes from all commits
8ca93a4
91c989e
857381d
2679c51
664cd95
1d5b291
600278f
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| julia 0.6 | ||
| LibCURL 0.2.2 | ||
| Compat 0.8.3 | ||
| Compat 0.41 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| # FTP code for when the file transfer is complete. | ||
| const complete_transfer_code = 226 | ||
|
|
||
| """ | ||
| FTP(; kwargs...) -> FTP | ||
|
|
||
|
|
@@ -110,6 +113,94 @@ function upload(ftp::FTP, local_file::IO, remote_name::AbstractString; mode::FTP | |
| end | ||
|
|
||
|
|
||
| """ | ||
| upload( | ||
| ftp::FTP, | ||
| local_file_paths::Vector{<:AbstractString}, | ||
| ftp_dir<:AbstractString; | ||
| retry_callback::Function=(count, options) -> (count < 4, options), | ||
| retry_wait_seconds::Integer = 5 | ||
| ) | ||
|
|
||
| Uploads the files specified in local_file_paths to the directory specifed by | ||
| ftp_dir. The files will have the same names. | ||
|
|
||
| By default, will try to deliver the files 4 times with a 5 second wait in between | ||
| each failed attempt. | ||
|
|
||
| You can specify a function for retry_callback to change behaviour. This function must | ||
| take as parameters the number of attempts that have been made so far, and the current | ||
| ftp connection options as a FTPClient.ConnContext type. It must return a boolean | ||
| that is true if another delivery attempt can be made, and a TPClient.ConnContext | ||
| type that is the connection options to use for all future files to be delivered. This | ||
| allows backup ftp directories to be used for example. | ||
|
|
||
| # Arguments | ||
| `ftp::FTP`: The FTP to deliver to. See FTPClient.FTP for details. | ||
| `file_paths::Vector{T}`: The file paths to the files we want to deliver. | ||
| `ftp_dir`: The directory on the ftp server where we want to drop the files. | ||
| `retry_callback::Function=(count, options) -> (count < 4, options)`: Function for retrying | ||
| when delivery fails. | ||
| `retry_wait_seconds::Integer = 5`: How many seconds to wait in between retries. | ||
|
|
||
| # Returns | ||
| - `Array{Bool,1}`: Returns a vector of booleans with true for each successfully delivered | ||
| file and false for any that failed to transfer. | ||
| """ | ||
| @compat function upload( | ||
| ftp::FTP, | ||
| local_file_paths::Vector{<:AbstractString}, | ||
| ftp_dir::AbstractString; | ||
| retry_callback::Function=(count, options) -> (count < 4, options), | ||
| retry_wait_seconds::Integer = 5 | ||
| ) | ||
|
|
||
| successful_delivery = Bool[] | ||
|
|
||
| ftp_options = ftp.ctxt | ||
|
|
||
| for single_file in local_file_paths | ||
| # The location we are going to drop the file in the FTP server | ||
| server_location = joinpath(ftp_dir, basename(single_file)) | ||
|
|
||
| # ftp_put requires an io so open up our file. | ||
| open(single_file) do single_file_io | ||
| # Whether or not the current file was successfully delivered to the FTP | ||
| file_delivery_success = false | ||
|
|
||
| attempts = 1 | ||
| # The loops should break after an appropriate amount of retries. | ||
| # This way of doing retries makes testing easier. | ||
| # Defaults to 4 attempts, waiting 5 seconds between each retry. | ||
| while true | ||
| try | ||
| resp = ftp_put(ftp_options, server_location, single_file_io) | ||
| file_delivery_success = resp.code == complete_transfer_code | ||
| if file_delivery_success | ||
| break | ||
| end | ||
| catch e | ||
| warn(e) | ||
| end | ||
| sleep(retry_wait_seconds) | ||
| # It returns ftp_options for testing purposes, where the ftp server | ||
| # starts not existing then comes into existence during retries. | ||
| do_retry, ftp_options = retry_callback(attempts, ftp_options) | ||
| if !do_retry | ||
| break | ||
| end | ||
| attempts += 1 | ||
| end | ||
|
|
||
| push!(successful_delivery, file_delivery_success) | ||
|
Contributor
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. Maybe just
Contributor
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. I'm not sure what your suggestion is here. The function is attempting to upload multiple files so returns
Contributor
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. I stand corrected. I double checked this but I missed the for loop. Sorry for the noise
Contributor
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. Fixed |
||
|
|
||
| end | ||
| end | ||
|
|
||
| return successful_delivery | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| readdir(ftp::FTP) | ||
|
|
||
|
|
||
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.
You should set the minimum required version of Compat to v0.41 (in the REQUIRE file). This is the first version of Compat that includes
NothingandCvoid.