Method: Mir::Application#pull

Defined in:
lib/mir/application.rb

#pull(target) ⇒ void

This method returns an undefined value.

Copy the remote disk contents into the specified directory



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mir/application.rb', line 107

def pull(target)
  write_dir = Utils.try_create_dir(target)
  Mir.logger.info "Copying remote disk to #{write_dir.path} using #{config.max_threads} threads"
  failed_downloads = []
  
  time = Benchmark.measure do 
    queue = WorkQueue.new(config.max_threads)
    
    Models::Resource.ordered_groups(DEFAULT_BATCH_SIZE) do |resources|
      # Track the number of download attempts made per resource
      batch = resources.inject({}) { |set, resource| set[resource] = 0; set }
      
      while !batch.empty? do
        batch.each do |resource, attempts|
          dest = File.join(write_dir.path, resource.filename)
          if resource.is_directory?
            Utils.try_create_dir(dest)
            batch.delete(resource)
          elsif attempts >= config.max_download_attempts
            Mir.logger.info "Resource #{resource.abs_path} failed to download"
            failed_downloads << resource
            batch.delete(resource)
          elsif resource.synchronized? dest
            Mir.logger.debug "Skipping already downloaded file #{resource.abs_path}"
            batch.delete(resource)
          else
            batch[resource] += 1
            queue.enqueue_b do
              Mir.logger.debug "Beginning download of #{resource.abs_path}"
              disk.copy(resource.abs_path, dest)
              if resource.synchronized? dest
                FileUtils.chmod_R 0755, dest # allow binaries to execute
                puts "Pulled #{dest}"
                batch.delete(resource)
              end
            end
          end
        end
        queue.join
      end
    end
  end
  Mir.logger.info time
  puts "Completed pull operation #{time}"
  unless failed_downloads.empty?
    puts "The following files failed to download after several attempts:\n}"
    puts failed_downloads.join("\n")
  end
end