lib/filbunke/client.rb in filbunke-1.13.2 vs lib/filbunke/client.rb in filbunke-1.13.3

- old
+ new

@@ -238,10 +238,12 @@ return {} end end def update_http_file!(file, local_file_path) + downloaded_file = nil + tmp_filename = "#{local_file_path}-#{(0...8).map { (65 + rand(26)).chr }.join}.tmp" begin async_request = if @repository.user Typhoeus::Request.new( URI.escape(file.url), :followlocation => true, @@ -253,12 +255,11 @@ URI.escape(file.url), :followlocation => true ) end - downloaded_file = nil - tmp_filename = "#{local_file_path}-#{(0...8).map { (65 + rand(26)).chr }.join}.tmp" + async_request.on_headers do |response| if response.code != 200 raise "Failed to fetch response(#{response.code}) for url '#{response.effective_url}' ---\n\t #{response.inspect}" end @logger.debug("Updating: #{local_file_path}") @@ -272,22 +273,27 @@ async_request.on_complete do |response| unless downloaded_file.nil? downloaded_file.close end - if response.code == 200 - ::FileUtils.mv(tmp_filename, local_file_path) - else - ::FileUtils.rm(tmp_filename) if ::File.exist?(tmp_filename) + if ::File.exist?(tmp_filename) + if response.code == 200 + ::FileUtils.mv(tmp_filename, local_file_path) + else + ::FileUtils.rm(tmp_filename) + end end true end @hydra.queue async_request @async_requests << async_request true rescue RuntimeError, SystemCallError, StandardError => e msg = ["#{e.class} - #{e.message}", *e.backtrace].join("\n\t") @logger.error "Failed to update file #{file.url}: #{msg}" + if ::File.exist?(tmp_filename) + ::FileUtils.rm(tmp_filename) + end false end end def update_hdfs_file!(file, local_file_path)