lib/filbunke/client.rb in filbunke-1.7.3 vs lib/filbunke/client.rb in filbunke-1.8.0

- old
+ new

@@ -10,14 +10,15 @@ URL_KEY = 'url' FROM_CHECKPOINT_KEY = 'from_checkpoint' HASH_KEY = 'hash' - def initialize(repository, logger, callbacks = []) + 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 def with_updated_files(last_checkpoint) updates = get_updated_file_list(last_checkpoint) updated_files = updates["files"] || [] @@ -64,31 +65,41 @@ def update_files!(last_checkpoint) with_updated_files(last_checkpoint) {} end def register_updated_file!(path, url, hash = nil) - register_http = Net::HTTP.new(@repository.host, @repository.port) - register_http.start do |http| - register_path = "/#{FILES_ACTION}/#{@repository.name}/#{path}?#{URL_KEY}=#{url}" - register_path += "&#{HASH_KEY}=#{hash}" if hash - request = Net::HTTP::Put.new(register_path) - response = http.request(request) - if response.code.to_i != 204 - raise "Failed to register updated file: #{path}" + 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 def register_deleted_file!(path) - register_http = Net::HTTP.new(@repository.host, @repository.port) - register_http.start do |http| - register_path = "/#{FILES_ACTION}/#{@repository.name}/#{path}" - request = Net::HTTP::Delete.new(register_path) - response = http.request(request) - if response.code.to_i != 204 - raise "Failed to 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 def touch_repository! touch_http = Net::HTTP.new(@repository.host, @repository.port) @@ -114,9 +125,17 @@ return response.body.chomp.to_i end end private + + def log_failed_request(failed_request_command, e) + return unless @failed_request_log_file_name + ::File.open(@failed_request_log_file_name, 'a+') do |f| + f.puts("# #{Time.now}: #{e.message if e}") + f.puts(failed_request_command) + end + end def update_file!(file, local_file_path) if file.url =~ /^http:\/\// update_http_file!(file, local_file_path)