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
-
#initialize(repository, logger, callbacks = [], failed_request_log_file_name = nil) ⇒ Client
constructor
A new instance of Client.
- #last_checkpoint ⇒ Object
- #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 |
# 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 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
#last_checkpoint ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/filbunke/client.rb', line 117 def 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(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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/filbunke/client.rb', line 88 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(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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/filbunke/client.rb', line 70 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(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
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/filbunke/client.rb', line 105 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(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
66 67 68 |
# File 'lib/filbunke/client.rb', line 66 def update_files!(last_checkpoint) with_updated_files(last_checkpoint) {} end |
#with_updated_files(last_checkpoint) ⇒ Object
22 23 24 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 |
# File 'lib/filbunke/client.rb', line 22 def with_updated_files(last_checkpoint) updates = get_updated_file_list(last_checkpoint) updated_files = updates["files"] || [] failure = false new_checkpoint = updates["checkpoint"] @logger.log "Updating repository: #{repository.name}: #{updated_files.size} files. Checkpoint: #{last_checkpoint} ==> #{new_checkpoint}" if updated_files.size > 0 @hydra = Typhoeus::Hydra.new(:max_concurrency => 10) @async_requests = [] 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) run_callbacks_delete(file, local_file_path) else if file_needs_update?(file, local_file_path) if update_file!(file, local_file_path) then yield file else @logger.log "Unable to get file #{file.url} ==> #{file.path}!" failure = true end else @logger.log "File exists with correct hash: #{local_file_path}" run_callbacks_no_change(file, local_file_path) end end end @hydra.run failure = failure || @async_requests.any?{|request| request.handled_response == false } failure ? last_checkpoint : (new_checkpoint || last_checkpoint) end |