diff options
author | elijah <elijah@riseup.net> | 2016-07-01 14:49:27 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2016-07-01 14:49:27 -0700 |
commit | 174dbf380fe1b0962a9e173caa6dd0a0f76a14e1 (patch) | |
tree | d2f1414652d2c0fbd1003347c2abf2be1ffff473 /vendor/rsync_command/lib/rsync_command.rb | |
parent | 2d277c7d2aa3ddfbe3c2c65f68d7290930934b70 (diff) | |
parent | f9284ff22da6eec685782dbc9aa4a4ded0efec29 (diff) |
Merge branch 'feature/newcli' into develop
Diffstat (limited to 'vendor/rsync_command/lib/rsync_command.rb')
-rw-r--r-- | vendor/rsync_command/lib/rsync_command.rb | 61 |
1 files changed, 49 insertions, 12 deletions
diff --git a/vendor/rsync_command/lib/rsync_command.rb b/vendor/rsync_command/lib/rsync_command.rb index 39e5945..bdcafe0 100644 --- a/vendor/rsync_command/lib/rsync_command.rb +++ b/vendor/rsync_command/lib/rsync_command.rb @@ -4,6 +4,44 @@ require "rsync_command/thread_pool" require 'monitor' +class RsyncRunner + attr_accessor :logger + attr_accessor :source, :dest, :flags, :includes, :excludes + attr_accessor :user, :host + attr_accessor :chdir, :ssh + def initialize(rsync_command) + @logger = nil + @source = "" + @dest = "" + @flags = "" + @includes = [] + @excludes = [] + @rsync_command = rsync_command + end + def log(*args) + @logger.log(*args) + end + def valid? + !@source.empty? || !@dest.empty? + end + def to_hash + fields = [:flags, :includes, :excludes, :logger, :ssh, :chdir] + fields.inject({}){|hsh, i| + hsh[i] = self.send(i); hsh + } + end + def exec + return unless valid? + dest = { + :user => self.user, + :host => self.host, + :path => self.dest + } + src = self.source + @rsync_command.exec_rsync(src, dest, self.to_hash) + end +end + class RsyncCommand attr_accessor :failures, :logger @@ -21,15 +59,23 @@ class RsyncCommand def asynchronously(array, &block) pool = ThreadPool.new array.each do |item| - pool.schedule(item, &block) + pool.schedule(RsyncRunner.new(self), item, &block) end pool.shutdown end # + # returns true if last exec returned a failure + # + def failed? + @failures && @failures.any? + end + + # # runs rsync, recording failures # - def exec(src, dest, options={}) + def exec_rsync(src, dest, options={}) + logger = options[:logger] || @logger @failures.synchronize do @failures.clear end @@ -37,7 +83,7 @@ class RsyncCommand if options[:chdir] rsync_cmd = "cd '#{options[:chdir]}'; #{rsync_cmd}" end - @logger.debug rsync_cmd if @logger + logger.debug rsync_cmd if logger ok = system(rsync_cmd) unless ok @failures.synchronize do @@ -47,13 +93,6 @@ class RsyncCommand end # - # returns true if last exec returned a failure - # - def failed? - @failures && @failures.any? - end - - # # build rsync command # def command(src, dest, options={}) @@ -70,8 +109,6 @@ class RsyncCommand "rsync #{flags.compact.join(' ')} #{src} #{dest}" end - private - # # Creates an rsync location if the +address+ is a hash with keys :user, :host, and :path # (each component is optional). If +address+ is a string, we just pass it through. |