Skip to content

Commit

Permalink
added optional argument: :bwlimit
Browse files Browse the repository at this point in the history
  • Loading branch information
seanhuber committed Sep 9, 2017
1 parent 5870655 commit e18c847
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The constructor accepts the following named arguments:
| `:subdirs_only` | Optional | `Boolean` | An option to have `rsync` only sync files that are in subfolders of `:src_dir`. Defaults to `false`. |
| `:log_dir` | Optional | `String` | `rsync_wrapper` has `rsync` pipe its results into a logfile so that the ruby code can then parse this file (and then deletes it). This option should be set to the absolute path of the directory where the temporary logfile will be stored. |
| `:logfile` | Optional | `String` | If the `:log_dir` is not specified, you can provide an explicit path for `rsync_wrapper`'s temporary logfile. |
| `:bwlimit`|Optional|`Integer`|From rsync's man pages: This option allows you to specify a maximum transfer rate in kilobytes per second. This option is most effective when using rsync with large files (several megabytes and up). Due to the nature of rsync transfers, blocks of data are sent, then if rsync determines the transfer was too fast, it will wait before sending the next data block. The result is an average transfer rate equaling the specified limit. A value of zero specifies no limit.|

To execute `rsync`, invoke the `sync!` method which accepts a block with 2 parameters:

Expand All @@ -50,8 +51,8 @@ To execute `rsync`, invoke the `sync!` method which accepts a block with 2 param
Example:

```ruby
source_directory = '/Users/seanhuber/Documents/my_pdfs'
destination_directory = '/Users/seanhuber/Documents/my_pdfs (copied)'
source_directory = '/path/of/directory/you/want/to/sync'
destination_directory = '/path/of/directory/to/sync/to'

rsync = Rsync.new(src_dir: source_directory, dest_dir: destination_directory)

Expand Down
8 changes: 8 additions & 0 deletions lib/rsync_wrapper/rsync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def initialize **opts
else
File.join(Dir.pwd, "rsync-#{SecureRandom.uuid}.log")
end

@optional_arguments = [
:bwlimit,
# TODO: add rsync's other arguments that wouldn't interfere with the design of this gem https://linux.die.net/man/1/rsync
].map{|arg| opts[arg] ? [arg, opts[arg]] : nil}.compact.to_h
end

def sync! &block
Expand All @@ -31,6 +36,9 @@ def exec_rsync
rsync_opts = ['-ri', "--log-file '#{@logfile}'", '--size-only', '--prune-empty-dirs']
rsync_opts += @inclusions.map{|inc| "--include '#{inc}'"}
rsync_opts += @exclusions.map{|exc| "--exclude '#{exc}'"}

rsync_opts += @optional_arguments.map{|k,v| "--#{k}=#{v}"}

cmd = "rsync #{rsync_opts.join(' ')} \"#{@dirs[:src_dir]}\" \"#{@dirs[:dest_dir]}\" > /dev/null 2>&1"
`#{cmd}`
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rsync_wrapper/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RsyncWrapper
VERSION = '1.0.1'
VERSION = '1.1.0'
end
15 changes: 15 additions & 0 deletions spec/rsync_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@

FileUtils.rm_rf dest_dir
end

it 'accepts a bwlimit optional argument' do
rsync = Rsync.new(
src_dir: File.join(FileUtils.pwd, 'spec', 'test_src_dir'),
dest_dir: File.join(FileUtils.pwd, 'spec', 'test_dest_dir'),
include_extensions: [:doc, :docx, :pdf],
subdirs_only: true,
logfile: File.join(FileUtils.pwd, 'spec', 'dummy_rsync_output.log'),
bwlimit: 10000
)
expect(rsync).to receive(:`).with("rsync -ri --log-file '#{FileUtils.pwd}/spec/dummy_rsync_output.log' --size-only --prune-empty-dirs --include '*.doc' --include '*.docx' --include '*.pdf' --include '*/' --exclude '*' --bwlimit=10000 \"#{FileUtils.pwd}/spec/test_src_dir\" \"#{FileUtils.pwd}/spec/test_dest_dir\" > /dev/null 2>&1")

expect(rsync).to receive(:parse_logfile).and_return(nil)
rsync.sync!
end
end

0 comments on commit e18c847

Please sign in to comment.