Skip to content

Commit 8893955

Browse files
authored
Changes related to the new task API (#276)
* First verison of the new task API * Create task.rb * Fix linter error * Add tests for client.wait_for_task * Fix linter errors
1 parent de3e481 commit 8893955

23 files changed

+894
-669
lines changed

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ delete_documents_1: |-
4444
search_post_1: |-
4545
client.index('movies').search('american ninja')
4646
get_update_1: |-
47-
client.index('movies').get_update_status(1)
47+
client.index('movies').task(1)
4848
get_all_updates_1: |-
49-
client.index('movies').get_all_update_status
49+
client.index('movies').tasks
5050
get_keys_1: |-
5151
client.keys
5252
get_settings_1: |-

.rubocop_todo.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2021-11-21 21:31:55 UTC using RuboCop version 1.23.0.
3+
# on 2021-12-13 22:26:31 UTC using RuboCop version 1.23.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
@@ -20,23 +20,23 @@ Layout/HeredocIndentation:
2020
Exclude:
2121
- 'spec/meilisearch/index/documents_spec.rb'
2222

23-
# Offense count: 32
23+
# Offense count: 33
2424
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
2525
# IgnoredMethods: refine
2626
Metrics/BlockLength:
27-
Max: 512
27+
Max: 557
2828

2929
# Offense count: 1
3030
# Configuration parameters: CountComments, CountAsOne.
3131
Metrics/ClassLength:
32-
Max: 289
32+
Max: 278
3333

3434
# Offense count: 1
3535
# Configuration parameters: Max, CountKeywordArgs.
3636
Metrics/ParameterLists:
3737
MaxOptionalParameters: 4
3838

39-
# Offense count: 2
39+
# Offense count: 1
4040
Naming/AccessorMethodName:
4141
Exclude:
4242
- 'lib/meilisearch/index.rb'

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ documents = [
9292
{ id: 6, title: 'Philadelphia', genres: ['Drama'] },
9393
]
9494
# If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
95-
index.add_documents(documents) # => { "updateId": 0 }
95+
index.add_documents(documents) # => { "uid": 0 }
9696
```
9797

98-
With the `updateId`, you can check the status (`enqueued`, `processing`, `processed` or `failed`) of your documents addition using the [update endpoint](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).
98+
With the `uid`, you can check the status (`enqueued`, `processing`, `succeeded` or `failed`) of your documents addition using the [update endpoint](https://docs.meilisearch.com/reference/api/updates.html#get-an-update-status).
9999

100100
💡 To customize the `Client`, for example, increasing the default timeout, please check out [this section](https://github.com/meilisearch/meilisearch-ruby/wiki/Client-Options) of the Wiki.
101101

lib/meilisearch.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'meilisearch/version'
44
require 'meilisearch/utils'
5+
require 'meilisearch/task'
56
require 'meilisearch/client'
67
require 'meilisearch/index'
78

lib/meilisearch/client.rb

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,41 @@ def indexes
2222
def create_index(index_uid, options = {})
2323
body = Utils.transform_attributes(options.merge(uid: index_uid))
2424

25-
index_hash = http_post '/indexes', body
26-
index_object(index_hash['uid'], index_hash['primaryKey'])
25+
http_post '/indexes', body
2726
end
2827

29-
def get_or_create_index(index_uid, options = {})
30-
begin
31-
index_instance = fetch_index(index_uid)
32-
rescue ApiError => e
33-
raise e unless e.code == 'index_not_found'
34-
35-
index_instance = create_index(index_uid, options)
36-
end
37-
index_instance
28+
# Synchronous version of create_index.
29+
# Waits for the task to be achieved, be careful when using it.
30+
def create_index!(index_uid, options = {})
31+
task = create_index(index_uid, options)
32+
wait_for_task(task['uid'])
3833
end
3934

35+
# def get_or_create_index(index_uid, options = {})
36+
# begin
37+
# index_instance = fetch_index(index_uid)
38+
# rescue ApiError => e
39+
# raise e unless e.code == 'index_not_found'
40+
41+
# index_instance = create_index(index_uid, options)
42+
# end
43+
# index_instance
44+
# end
45+
4046
def delete_index(index_uid)
4147
index_object(index_uid).delete
4248
end
4349

44-
# Usage:
45-
# client.delete_index_if_exists('indexUID')
46-
def delete_index_if_exists(index_uid)
47-
index_object(index_uid).delete
48-
true
49-
rescue ApiError => e
50-
raise e if e.code != 'index_not_found'
50+
# # Usage:
51+
# # client.delete_index_if_exists('indexUID')
52+
# def delete_index_if_exists(index_uid)
53+
# index_object(index_uid).delete
54+
# true
55+
# rescue ApiError => e
56+
# raise e if e.code != 'index_not_found'
5157

52-
false
53-
end
58+
# false
59+
# end
5460

5561
# Usage:
5662
# client.index('indexUID')
@@ -107,10 +113,28 @@ def dump_status(dump_uid)
107113
end
108114
alias get_dump_status dump_status
109115

116+
### TASKS
117+
118+
def tasks
119+
task_endpoint.global_tasks
120+
end
121+
122+
def task(task_uid)
123+
task_endpoint.global_task(task_uid)
124+
end
125+
126+
def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
127+
task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms)
128+
end
129+
110130
private
111131

112132
def index_object(uid, primary_key = nil)
113133
Index.new(uid, @base_url, @api_key, primary_key, @options)
114134
end
135+
136+
def task_endpoint
137+
Task.new(@base_url, @api_key, @options)
138+
end
115139
end
116140
end

lib/meilisearch/index.rb

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# frozen_string_literal: true
22

33
require 'meilisearch/http_request'
4-
require 'timeout'
54

65
module MeiliSearch
76
class Index < HTTPRequest
87
attr_reader :uid, :primary_key, :created_at, :updated_at
98

109
def initialize(index_uid, url, api_key = nil, primary_key = nil, options = {})
10+
@url = url
1111
@uid = index_uid
1212
@primary_key = primary_key
1313
super(url, api_key, options)
@@ -31,10 +31,7 @@ def fetch_raw_info
3131
end
3232

3333
def update(body)
34-
index_hash = http_put indexes_path(id: @uid), Utils.transform_attributes(body)
35-
set_base_properties index_hash
36-
37-
self
34+
http_put indexes_path(id: @uid), Utils.transform_attributes(body)
3835
end
3936

4037
alias update_index update
@@ -78,8 +75,8 @@ def add_documents(documents, primary_key = nil)
7875
alias add_or_replace_documents add_documents
7976

8077
def add_documents!(documents, primary_key = nil)
81-
update = add_documents(documents, primary_key)
82-
wait_for_pending_update(update['updateId'])
78+
task = add_documents(documents, primary_key)
79+
wait_for_task(task['uid'])
8380
end
8481
alias replace_documents! add_documents!
8582
alias add_or_replace_documents! add_documents!
@@ -112,41 +109,41 @@ def update_documents(documents, primary_key = nil)
112109
alias add_or_update_documents update_documents
113110

114111
def update_documents!(documents, primary_key = nil)
115-
update = update_documents(documents, primary_key)
116-
wait_for_pending_update(update['updateId'])
112+
task = update_documents(documents, primary_key)
113+
wait_for_task(task['uid'])
117114
end
118115
alias add_or_update_documents! update_documents!
119116

120117
def add_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
121-
update_ids = []
118+
tasks = []
122119
documents.each_slice(batch_size) do |batch|
123-
update_ids.append(add_documents(batch, primary_key))
120+
tasks.append(add_documents(batch, primary_key))
124121
end
125-
update_ids
122+
tasks
126123
end
127124

128125
def add_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
129-
update_ids = add_documents_in_batches(documents, batch_size, primary_key)
126+
tasks = add_documents_in_batches(documents, batch_size, primary_key)
130127
responses = []
131-
update_ids.each do |update_object|
132-
responses.append(wait_for_pending_update(update_object['updateId']))
128+
tasks.each do |task_obj|
129+
responses.append(wait_for_task(task_obj['uid']))
133130
end
134131
responses
135132
end
136133

137134
def update_documents_in_batches(documents, batch_size = 1000, primary_key = nil)
138-
update_ids = []
135+
tasks = []
139136
documents.each_slice(batch_size) do |batch|
140-
update_ids.append(update_documents(batch, primary_key))
137+
tasks.append(update_documents(batch, primary_key))
141138
end
142-
update_ids
139+
tasks
143140
end
144141

145142
def update_documents_in_batches!(documents, batch_size = 1000, primary_key = nil)
146-
update_ids = update_documents_in_batches(documents, batch_size, primary_key)
143+
tasks = update_documents_in_batches(documents, batch_size, primary_key)
147144
responses = []
148-
update_ids.each do |update_object|
149-
responses.append(wait_for_pending_update(update_object['updateId']))
145+
tasks.each do |task_obj|
146+
responses.append(wait_for_task(task_obj['uid']))
150147
end
151148
responses
152149
end
@@ -161,8 +158,8 @@ def delete_documents(documents_ids)
161158
alias delete_multiple_documents delete_documents
162159

163160
def delete_documents!(documents_ids)
164-
update = delete_documents(documents_ids)
165-
wait_for_pending_update(update['updateId'])
161+
task = delete_documents(documents_ids)
162+
wait_for_task(task['uid'])
166163
end
167164
alias delete_multiple_documents! delete_documents!
168165

@@ -173,8 +170,8 @@ def delete_document(document_id)
173170
alias delete_one_document delete_document
174171

175172
def delete_document!(document_id)
176-
update = delete_document(document_id)
177-
wait_for_pending_update(update['updateId'])
173+
task = delete_document(document_id)
174+
wait_for_task(task['uid'])
178175
end
179176
alias delete_one_document! delete_document!
180177

@@ -183,8 +180,8 @@ def delete_all_documents
183180
end
184181

185182
def delete_all_documents!
186-
update = delete_all_documents
187-
wait_for_pending_update(update['updateId'])
183+
task = delete_all_documents
184+
wait_for_task(task['uid'])
188185
end
189186

190187
### SEARCH
@@ -195,31 +192,23 @@ def search(query, options = {})
195192
http_post "/indexes/#{@uid}/search", parsed_options
196193
end
197194

198-
### UPDATES
195+
### TASKS
199196

200-
def get_update_status(update_id)
201-
http_get "/indexes/#{@uid}/updates/#{update_id}"
197+
def task_endpoint
198+
Task.new(@url, @api_key, @options)
202199
end
200+
private :task_endpoint
203201

204-
def get_all_update_status
205-
http_get "/indexes/#{@uid}/updates"
202+
def task(task_uid)
203+
task_endpoint.index_task(@uid, task_uid)
206204
end
207205

208-
def achieved_upate?(update)
209-
update['status'] != 'enqueued' && update['status'] != 'processing'
206+
def tasks
207+
task_endpoint.index_tasks(@uid)
210208
end
211209

212-
def wait_for_pending_update(update_id, timeout_in_ms = 5000, interval_in_ms = 50)
213-
Timeout.timeout(timeout_in_ms.to_f / 1000) do
214-
loop do
215-
get_update = get_update_status(update_id)
216-
return get_update if achieved_upate?(get_update)
217-
218-
sleep interval_in_ms.to_f / 1000
219-
end
220-
end
221-
rescue Timeout::Error
222-
raise MeiliSearch::TimeoutError
210+
def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
211+
task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms)
223212
end
224213

225214
### STATS
@@ -236,10 +225,6 @@ def indexing?
236225
stats['isIndexing']
237226
end
238227

239-
def last_update
240-
stats['lastUpdate']
241-
end
242-
243228
def field_distribution
244229
stats['fieldDistribution']
245230
end

lib/meilisearch/task.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'meilisearch/http_request'
4+
require 'timeout'
5+
6+
module MeiliSearch
7+
class Task < HTTPRequest
8+
def global_tasks
9+
http_get '/tasks/'
10+
end
11+
12+
def global_task(task_uid)
13+
http_get "/tasks/#{task_uid}"
14+
end
15+
16+
def index_tasks(index_uid)
17+
http_get "/indexes/#{index_uid}/tasks"
18+
end
19+
20+
def index_task(index_uid, task_uid)
21+
http_get "/indexes/#{index_uid}/tasks/#{task_uid}"
22+
end
23+
24+
def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
25+
Timeout.timeout(timeout_in_ms.to_f / 1000) do
26+
loop do
27+
task = global_task(task_uid)
28+
return task if achieved_task?(task)
29+
30+
sleep interval_in_ms.to_f / 1000
31+
end
32+
end
33+
rescue Timeout::Error
34+
raise MeiliSearch::TimeoutError
35+
end
36+
37+
private
38+
39+
def achieved_task?(task)
40+
task['status'] != 'enqueued' && task['status'] != 'processing'
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)