Skip to content

Commit

Permalink
perform a status test when initializing the solr connector
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Feb 17, 2024
1 parent ec99ed8 commit ca32e8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/goo/search/solr/solr_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ def admin_url
"#{@solr_url}/admin"
end

def solr_alive?
collections_url = URI.parse("#{admin_url}/collections?action=CLUSTERSTATUS")
http = Net::HTTP.new(collections_url.host, collections_url.port)
request = Net::HTTP::Get.new(collections_url.request_uri)

begin
response = http.request(request)
return response.code.eql?("200") && JSON.parse(response.body).dig("responseHeader", "status").eql?(0)
rescue StandardError => e
return false

Check warning on line 17 in lib/goo/search/solr/solr_admin.rb

View check run for this annotation

Codecov / codecov/patch

lib/goo/search/solr/solr_admin.rb#L16-L17

Added lines #L16 - L17 were not covered by tests
end
end

def fetch_all_collections
collections_url = URI.parse("#{admin_url}/collections?action=LIST")

Expand Down
11 changes: 11 additions & 0 deletions lib/goo/search/solr/solr_connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ def initialize(solr_url, collection_name)
@solr_url = solr_url
@collection_name = collection_name
@solr = RSolr.connect(url: collection_url)

# Perform a status test and wait up to 30 seconds before raising an error
wait_time = 0
max_wait_time = 30
until solr_alive? || wait_time >= max_wait_time
sleep 1
wait_time += 1

Check warning on line 23 in lib/goo/search/solr/solr_connector.rb

View check run for this annotation

Codecov / codecov/patch

lib/goo/search/solr/solr_connector.rb#L22-L23

Added lines #L22 - L23 were not covered by tests
end
raise "Solr instance not reachable within #{max_wait_time} seconds" unless solr_alive?


@custom_schema = false
end

Expand Down

0 comments on commit ca32e8c

Please sign in to comment.