Class: Filbunke::Client
- Inherits:
-
Object
- Object
- Filbunke::Client
- Defined in:
- lib/filbunke/client.rb
Constant Summary collapse
- UPDATES_ACTION =
'updates'
- FILES_ACTION =
'files'
- LAST_CHECKPOINT_ACTION =
'last_checkpoint'
- TOUCH_ACTION =
'touch'
- URL_KEY =
'url'
- FROM_CHECKPOINT_KEY =
'from_checkpoint'
- HASH_KEY =
'hash'
Instance Attribute Summary collapse
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Instance Method Summary collapse
- #fetch_remote_last_checkpoint ⇒ Object
-
#initialize(repository, logger, callbacks = [], failed_request_log_file_name = nil) ⇒ Client
constructor
A new instance of Client.
- #register_deleted_file!(path) ⇒ Object
- #register_updated_file!(path, url, hash = nil) ⇒ Object
- #touch_repository! ⇒ Object
- #update_files!(last_checkpoint) ⇒ Object
- #with_updated_files(last_checkpoint) ⇒ Object
Constructor Details
#initialize(repository, logger, callbacks = [], failed_request_log_file_name = nil) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 |
# File 'lib/filbunke/client.rb', line 15 def initialize(repository, logger, callbacks = [], failed_request_log_file_name = nil) @repository = repository @logger = logger @callbacks = callbacks @failed_request_log_file_name = failed_request_log_file_name @hydra = Typhoeus::Hydra.new(:max_concurrency => @repository.hydra_concurrency) @logger.info "initialized client for repository '#{@repository.name}'; #{@repository.inspect}" end |
Instance Attribute Details
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
4 5 6 |
# File 'lib/filbunke/client.rb', line 4 def repository @repository end |
Instance Method Details
#fetch_remote_last_checkpoint ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/filbunke/client.rb', line 162 def fetch_remote_last_checkpoint last_checkpoint_http = Net::HTTP.new(@repository.host, @repository.port) last_checkpoint_http.start do |http| last_checkpoint_path = "/#{UPDATES_ACTION}/#{@repository.name}/#{LAST_CHECKPOINT_ACTION}" request = Net::HTTP::Get.new(Addressable::URI.encode(last_checkpoint_path)) response = http.request(request) if response.code.to_i != 200 raise "Failed to get last checkpoint for repository: #{@repository.name}" end return response.body.chomp.to_i end end |
#register_deleted_file!(path) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/filbunke/client.rb', line 133 def register_deleted_file!(path) register_path = "/#{FILES_ACTION}/#{@repository.name}/#{path}" begin register_http = Net::HTTP.new(@repository.host, @repository.port) register_http.start do |http| request = Net::HTTP::Delete.new(Addressable::URI.encode(register_path)) response = http.request(request) if response.code.to_i != 204 raise "Failed to register deleted file: #{path}" end end rescue Exception => e log_failed_request(%Q{curl -XDELETE "http://#{@repository.host}:#{@repository.port}#{register_path}"}, e) raise e end end |
#register_updated_file!(path, url, hash = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/filbunke/client.rb', line 115 def register_updated_file!(path, url, hash = nil) register_path = "/#{FILES_ACTION}/#{@repository.name}/#{path}?#{URL_KEY}=#{url}" register_path += "&#{HASH_KEY}=#{hash}" if hash begin register_http = Net::HTTP.new(@repository.host, @repository.port) register_http.start do |http| request = Net::HTTP::Put.new(Addressable::URI.encode(register_path)) response = http.request(request) if response.code.to_i != 204 raise "Failed to register updated file: #{path}" end end rescue Exception => e log_failed_request(%Q{curl -XPUT "http://#{@repository.host}:#{@repository.port}#{register_path}"}, e) raise e end end |
#touch_repository! ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/filbunke/client.rb', line 150 def touch_repository! touch_http = Net::HTTP.new(@repository.host, @repository.port) touch_http.start do |http| touch_path = "/#{UPDATES_ACTION}/#{@repository.name}/#{TOUCH_ACTION}" request = Net::HTTP::Put.new(Addressable::URI.encode(touch_path)) response = http.request(request) if response.code.to_i != 204 raise "Failed to touch repository: #{@repository.name}" end end end |
#update_files!(last_checkpoint) ⇒ Object
111 112 113 |
# File 'lib/filbunke/client.rb', line 111 def update_files!(last_checkpoint) with_updated_files(last_checkpoint) {} end |
#with_updated_files(last_checkpoint) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/filbunke/client.rb', line 25 def with_updated_files(last_checkpoint) updates = get_updated_file_list(last_checkpoint) updated_files = updates["files"] || [] new_checkpoint = updates["checkpoint"] if updated_files.empty? if updates.key?("files") return begin fetch_remote_last_checkpoint rescue => e @logger.warn "Failed to fetch remote last_checkpoint #{@repository.name} will fall back to local last_checkpoint=#{last_checkpoint}" last_checkpoint end else return last_checkpoint end end @logger.info "Updating repository: #{@repository.name}: #{updated_files.size} files. Checkpoint: #{last_checkpoint} ==> #{new_checkpoint}" @async_requests = [] callbacks_on_update = [] callbacks_on_no_change = [] callbacks_on_delete = [] has_update_file_failure = false updated_files.each do |raw_file| file = File.new(raw_file) local_file_path = ::File.join(@repository.local_path, file.path) if file.state == "DELETED" then delete_file!(local_file_path) callbacks_on_delete << OpenStruct.new({ :file => file, :local_file_path => local_file_path }) else if file_needs_update?(file, local_file_path) if update_file!(file, local_file_path) then yield file callbacks_on_update << OpenStruct.new({ :file => file, :local_file_path => local_file_path }) else @logger.error "Unable to fetch file #{file.url} ==> #{file.path}!" has_update_file_failure = true break; end else @logger.debug "File exists with correct hash: #{local_file_path}" callbacks_on_no_change << OpenStruct.new({:file => file, :local_file_path => local_file_path}) end end end if has_update_file_failure @logger.error "FAILED to fetch files for #{@repository.name} last_checkpoint = #{last_checkpoint}" return last_checkpoint end @logger.info "Done setting up async requests for #{@repository.name}, starting fetch..." has_fetch_files_failure = begin @hydra.run @async_requests.any? do |request| @logger.warn "request did not handle response: #{request.inspect}" if request.response.nil? || request.response.code != 200 request.response.nil? || request.response.code != 200 end rescue RuntimeError, SystemCallError, StandardError => e msg = ["#{e.class} - #{e.}", *e.backtrace].join("\n\t") @logger.error "FAILED to fetch files for #{@repository.name} last_checkpoint = #{last_checkpoint}; #{msg}" true end if has_fetch_files_failure @logger.error "FAILED to update files for #{@repository.name} last_checkpoint = #{last_checkpoint}" return last_checkpoint end @logger.info "Done fetching files for #{@repository.name}, processing callbacks..." new_or_last_checkpoint = begin run_callbacks_delete(callbacks_on_delete) run_callbacks(callbacks_on_update) run_callbacks_no_change(callbacks_on_no_change) new_checkpoint || last_checkpoint rescue RuntimeError, SystemCallError, StandardError => e msg = ["#{e.class} - #{e.}", *e.backtrace].join("\n\t") @logger.error "FAILED to process callbacks for #{@repository.name} last_checkpoint = #{last_checkpoint}; #{msg}" last_checkpoint end new_or_last_checkpoint end |