Skip to content

Commit

Permalink
Replace HTTP with Faraday (because jruby + HTTP response streaming + …
Browse files Browse the repository at this point in the history
…apache.org don't seem to get along?)
  • Loading branch information
cbeer committed Sep 25, 2024
1 parent 608d677 commit 4573985
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/solr_wrapper/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'http'
require 'faraday'
require 'json'

module SolrWrapper
Expand All @@ -18,15 +18,15 @@ def exists?(core_or_collection_name)
private

def collection?(name)
response = HTTP.get("#{url}admin/collections?action=LIST&wt=json")
response = Faraday.get("#{url}admin/collections?action=LIST&wt=json")
data = JSON.parse(response.body)
return if data['error'] && data['error']['msg'] == 'Solr instance is not running in SolrCloud mode.'

data['collections'].include? name
end

def core?(name)
response = HTTP.get("#{url}admin/cores?action=STATUS&wt=json&core=#{name}")
response = Faraday.get("#{url}admin/cores?action=STATUS&wt=json&core=#{name}")
!JSON.parse(response.body)['status'][name].empty?
end
end
Expand Down
25 changes: 19 additions & 6 deletions lib/solr_wrapper/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'http'
require 'faraday'
require 'faraday/follow_redirects'

module SolrWrapper
class Configuration
Expand Down Expand Up @@ -110,18 +111,23 @@ def mirror_url
options[:mirror_url] + mirror_artifact_path
else
begin
json = HTTP.follow.get(closest_mirror_url).body
client = Faraday.new(closest_mirror_url) do |faraday|
faraday.use Faraday::FollowRedirects::Middleware
faraday.adapter Faraday.default_adapter
end

json = client.get.body
doc = JSON.parse(json)
url = doc['preferred'] + doc['path_info']

response = HTTP.head(url)
response = Faraday.head(url)

if response.status.success?
if response.success?
url
else
archive_download_url
end
rescue Errno::ECONNRESET, SocketError, HTTP::Error
rescue Errno::ECONNRESET, SocketError, Faraday::Error
archive_download_url
end
end
Expand Down Expand Up @@ -210,7 +216,14 @@ def default_configuration_paths
end

def fetch_latest_version
response = HTTP.follow.get(options.fetch(:latest_version_url, 'https://solr.apache.org/downloads.html'))
url = options.fetch(:latest_version_url, 'https://solr.apache.org/downloads.html')

client = Faraday.new(url) do |faraday|
faraday.use Faraday::FollowRedirects::Middleware
faraday.adapter Faraday.default_adapter
end

response = client.get
response.body.to_s[/Solr \d+\.\d+\.\d+/][/\d+\.\d+\.\d+/]
end
end
Expand Down
24 changes: 15 additions & 9 deletions lib/solr_wrapper/downloader.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
require 'ruby-progressbar'
require 'http'
require 'faraday'
require 'faraday/follow_redirects'

module SolrWrapper
class Downloader
def self.fetch_with_progressbar(url, output)
pbar = SafeProgressBar.new(title: File.basename(url), total: nil, format: '%t: |%B| %p%% (%e )')

response = HTTP.follow.get(url)
pbar.total = response.headers['content-length'].to_i
client = Faraday.new(url) do |faraday|
faraday.use Faraday::FollowRedirects::Middleware
faraday.adapter Faraday.default_adapter
end

File.open(output, 'wb') do |f|
response.body.each do |chunk|
f.write(chunk)
pbar.progress += chunk.length
client.get do |req|
req.options.on_data = Proc.new do |chunk, overall_received_bytes, env|
pbar.total = env.response_headers['content-length'].to_i
pbar.progress = overall_received_bytes
f.write(chunk)
end
end

nil
end
rescue HTTP::Error => e

true
rescue Faraday::Error => e
raise SolrWrapperError, "Unable to download solr from #{url}\n#{e}"
end

Expand Down
3 changes: 2 additions & 1 deletion solr_wrapper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "http"
spec.add_dependency "faraday"
spec.add_dependency "faraday-follow_redirects"
spec.add_dependency "minitar"
spec.add_dependency "ruby-progressbar"
spec.add_dependency "retriable"
Expand Down

0 comments on commit 4573985

Please sign in to comment.