Skip to content

Commit 84238b4

Browse files
eskombrobidoubiwa
authored andcommitted
Implement wait_for_pending_update method (#43)
* Wait for pending update method - Implement wait_for_pending_update method - Add tests for wait_for_pending_update * Lint auto-generated file * Custom error
1 parent 52ad06f commit 84238b4

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

.rubocop_todo.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-05-24 23:52:41 +0200 using RuboCop version 0.84.0.
3+
# on 2020-06-03 19:41:42 +0200 using RuboCop version 0.85.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 19
9+
# Offense count: 20
1010
# Configuration parameters: CountComments, ExcludedMethods.
1111
# ExcludedMethods: refine
1212
Metrics/BlockLength:
@@ -15,7 +15,7 @@ Metrics/BlockLength:
1515
# Offense count: 1
1616
# Configuration parameters: CountComments.
1717
Metrics/ClassLength:
18-
Max: 160
18+
Max: 171
1919

2020
# Offense count: 1
2121
# Configuration parameters: CountComments, ExcludedMethods.
@@ -27,6 +27,13 @@ Naming/AccessorMethodName:
2727
Exclude:
2828
- 'lib/meilisearch/index.rb'
2929

30-
# Offense count: 5
30+
# Offense count: 6
3131
Style/Documentation:
32-
Enabled: false
32+
Exclude:
33+
- 'spec/**/*'
34+
- 'test/**/*'
35+
- 'lib/meilisearch.rb'
36+
- 'lib/meilisearch/client.rb'
37+
- 'lib/meilisearch/error.rb'
38+
- 'lib/meilisearch/http_request.rb'
39+
- 'lib/meilisearch/index.rb'

lib/meilisearch/error.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
module MeiliSearch
44
class MeiliSearchError < StandardError; end
55
class IndexUidError < MeiliSearchError; end
6+
class MeiliSearchTimeoutError < MeiliSearchError
7+
attr_reader :message
8+
9+
def initialize
10+
@message = "MeiliSearchTimeoutError: update wasn't processed in the expected time"
11+
end
12+
13+
def to_s
14+
"#{@message}."
15+
end
16+
end
617

718
class HTTPError < MeiliSearchError
819
attr_reader :status

lib/meilisearch/index.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

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

56
module MeiliSearch
67
class Index < HTTPRequest
@@ -93,6 +94,19 @@ def get_all_update_status
9394
http_get "/indexes/#{@uid}/updates"
9495
end
9596

97+
def wait_for_pending_update(update_id, timeout_in_ms = 5000, interval_in_ms = 50)
98+
Timeout.timeout(timeout_in_ms.to_f / 1000) do
99+
loop do
100+
get_update = get_update_status(update_id)
101+
return get_update if get_update['status'] != 'enqueued'
102+
103+
sleep interval_in_ms.to_f / 1000
104+
end
105+
end
106+
rescue Timeout::Error
107+
raise MeiliSearch::MeiliSearchTimeoutError
108+
end
109+
96110
### STATS
97111

98112
def stats
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 MeiliSearchTimeoutError' 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(MeiliSearch::MeiliSearchTimeoutError)
46+
end
47+
48+
it 'waits for pending update with custom interval_in_ms and raises Timeout::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)