Skip to content

Commit d013e2d

Browse files
committed
Wait for pending update method
- Implement wait_for_pending_update method - Add tests for wait_for_pending_update
1 parent 56250ae commit d013e2d

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

lib/meilisearch/index.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# frozen_string_literal: true
22

33
require 'meilisearch/http_request'
4+
require 'timeout'
45

56
module MeiliSearch
6-
class Index < HTTPRequest
7+
class Index < HTTPRequest # rubocop:disable Metrics/ClassLength
78
attr_reader :uid
89

910
def initialize(index_uid, url, api_key = nil)
@@ -231,5 +232,16 @@ def accept_new_fields
231232
def update_accept_new_fields(accept_new_fields)
232233
http_post "/indexes/#{@uid}/settings/accept-new-fields", accept_new_fields
233234
end
235+
236+
def wait_for_pending_update(update_id, timeout_in_ms = 5000, interval_in_ms = 50)
237+
Timeout.timeout(timeout_in_ms.to_f / 1000) do
238+
loop do
239+
get_update = get_update_status(update_id)
240+
return get_update if get_update['status'] != 'enqueued'
241+
242+
sleep interval_in_ms.to_f / 1000
243+
end
244+
end
245+
end
234246
end
235247
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe MeiliSearch::Index do
4+
before(:all) do
5+
@documents = [
6+
{ objectId: 123, title: 'Pride and Prejudice' },
7+
{ objectId: 456, title: 'Le Petit Prince' },
8+
{ objectId: 1, title: 'Alice In Wonderland' },
9+
{ objectId: 1344, title: 'The Hobbit' },
10+
{ objectId: 4, title: 'Harry Potter and the Half-Blood Prince' },
11+
{ objectId: 42, title: 'The Hitchhiker\'s Guide to the Galaxy' }
12+
]
13+
client = MeiliSearch::Client.new($URL, $MASTER_KEY)
14+
clear_all_indexes(client)
15+
@index = client.create_index(uid: 'books', primaryKey: 'objectId')
16+
end
17+
18+
it 'waits for pending update with default values' do
19+
response = @index.add_documents(@documents)
20+
update_id = response['updateId']
21+
status = @index.wait_for_pending_update(update_id)
22+
expect(status).to be_a(Hash)
23+
expect(status['status']).not_to eq('enqueued')
24+
end
25+
26+
it 'waits for pending update with default values after several updates' do
27+
@index.add_documents(@documents)
28+
@index.add_documents(@documents)
29+
@index.add_documents(@documents)
30+
@index.add_documents(@documents)
31+
@index.add_documents(@documents)
32+
response = @index.add_documents(@documents)
33+
update_id = response['updateId']
34+
status = @index.wait_for_pending_update(update_id)
35+
expect(status).to be_a(Hash)
36+
expect(status['status']).not_to eq('enqueued')
37+
end
38+
39+
it 'waits for pending update with custom timeout_in_ms and raises error' do
40+
@index.add_documents(@documents)
41+
response = @index.add_documents(@documents)
42+
update_id = response['updateId']
43+
lambda {
44+
@index.wait_for_pending_update(update_id, 1)
45+
}.should raise_error(Timeout::Error)
46+
end
47+
48+
it 'waits for pending update with custom interval_in_ms and raises error' do
49+
@index.add_documents(@documents)
50+
response = @index.add_documents(@documents)
51+
update_id = response['updateId']
52+
lambda {
53+
Timeout.timeout(0.1) do
54+
@index.wait_for_pending_update(update_id, 5000, 200)
55+
end
56+
}.should raise_error(Timeout::Error)
57+
end
58+
end

0 commit comments

Comments
 (0)