Skip to content

Commit da629f4

Browse files
Merge #174
174: Add timeout errors to be kept when raise_on_failure is false r=brunoocasali a=brunoocasali Fixes #172 Co-authored-by: Bruno Casali <[email protected]>
2 parents 77fa50a + 0a93c05 commit da629f4

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/meilisearch-rails.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@ class SafeIndex
233233
def initialize(index_uid, raise_on_failure, options)
234234
client = MeiliSearch::Rails.client
235235
primary_key = options[:primary_key] || MeiliSearch::Rails::IndexSettings::DEFAULT_PRIMARY_KEY
236-
client.create_index(index_uid, { primaryKey: primary_key })
237-
@index = client.index(index_uid)
238236
@raise_on_failure = raise_on_failure.nil? || raise_on_failure
237+
238+
SafeIndex.log_or_throw(nil, @raise_on_failure) do
239+
client.create_index(index_uid, { primary_key: primary_key })
240+
end
241+
242+
@index = client.index(index_uid)
239243
end
240244

241245
::MeiliSearch::Index.instance_methods(false).each do |m|
@@ -273,7 +277,7 @@ def settings(*args)
273277

274278
def self.log_or_throw(method, raise_on_failure, &block)
275279
yield
276-
rescue ::MeiliSearch::ApiError => e
280+
rescue ::MeiliSearch::TimeoutError, ::MeiliSearch::ApiError => e
277281
raise e if raise_on_failure
278282

279283
# log the error

spec/integration_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,8 @@ class SerializedDocument < ActiveRecord::Base
13991399
end
14001400

14011401
describe 'Raise on failure' do
1402+
before { Vegetable.instance_variable_set('@ms_indexes', nil) }
1403+
14021404
it 'raises on failure' do
14031405
expect do
14041406
Fruit.search('', { filter: 'title = Nightshift' })
@@ -1410,6 +1412,33 @@ class SerializedDocument < ActiveRecord::Base
14101412
Vegetable.search('', { filter: 'title = Kale' })
14111413
end.not_to raise_error
14121414
end
1415+
1416+
context 'when Meilisearch server take too long to answer' do
1417+
let(:index_instance) { instance_double(MeiliSearch::Index, settings: nil, update_settings: nil) }
1418+
let(:slow_client) { instance_double(MeiliSearch::Client, index: index_instance) }
1419+
1420+
it 'does not raise error timeouts on reindex' do
1421+
allow(index_instance).to receive(:add_documents).and_raise(MeiliSearch::TimeoutError)
1422+
allow(slow_client).to receive(:create_index).and_return(index_instance)
1423+
1424+
allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)
1425+
1426+
expect do
1427+
Vegetable.create(name: 'potato')
1428+
end.not_to raise_error
1429+
end
1430+
1431+
it 'does not raise error timeouts on data addition' do
1432+
allow(slow_client).to receive(:create_index).and_raise(MeiliSearch::TimeoutError)
1433+
allow(index_instance).to receive(:add_documents).and_return(nil)
1434+
1435+
allow(MeiliSearch::Rails).to receive(:client).and_return(slow_client)
1436+
1437+
expect do
1438+
Vegetable.ms_reindex!
1439+
end.not_to raise_error
1440+
end
1441+
end
14131442
end
14141443

14151444
context "when have a internal class defined in the app's scope" do

0 commit comments

Comments
 (0)