-
Notifications
You must be signed in to change notification settings - Fork 93
Implement wait_for_pending_update method #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
c502a34
7a51285
8865df5
8784be9
0e1b016
b8e720f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import urllib | ||
from datetime import datetime | ||
from time import sleep | ||
from meilisearch._httprequests import HttpRequests | ||
|
||
# pylint: disable=R0904 | ||
|
@@ -181,6 +183,35 @@ def get_update_status(self, update_id): | |
) | ||
) | ||
|
||
|
||
def wait_for_pending_update(self, update_id, timeout_in_ms=2000, interval_in_ms=10): | ||
"""Wait until MeiliSearch processes an update, and get its status | ||
|
||
Parameters | ||
---------- | ||
update_id: int | ||
identifier of the update to retrieve | ||
timeout_in_ms (optional): int | ||
time the function should wait before rising a TimeoutError | ||
eskombro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
interval_in_ms (optional): int | ||
time interval the function should wait (sleep) between requests | ||
eskombro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Returns | ||
---------- | ||
update: `list` | ||
List of all enqueued and processed actions of the index. | ||
""" | ||
eskombro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
start_time = datetime.now() | ||
elapsed_time = 0 | ||
while elapsed_time < timeout_in_ms: | ||
get_update = self.get_update_status(update_id) | ||
if get_update['status'] != 'enqueued': | ||
return get_update | ||
sleep(interval_in_ms/1000) | ||
eskombro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
time_delta = datetime.now() - start_time | ||
elapsed_time = time_delta.seconds * 1000 + time_delta.microseconds / 1000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this line do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses the timedelta object to calculate the number of milliseconds since the moment the user called the wait_for_pending_update method. time_delta is an instance of a timedelta object, this just converts it to an int in ms |
||
raise TimeoutError | ||
|
||
|
||
eskombro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def get_stats(self): | ||
"""Get stats of an index | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from datetime import datetime | ||
import json | ||
import pytest | ||
import meilisearch | ||
from meilisearch.tests import BASE_URL, MASTER_KEY | ||
|
||
class TestUpdate: | ||
client = meilisearch.Client(BASE_URL, MASTER_KEY) | ||
index = None | ||
|
||
def setup_class(self): | ||
self.index = self.client.create_index(uid='indexUID') | ||
|
||
def teardown_class(self): | ||
self.index.delete() | ||
|
||
def test_wait_for_pending_update_default(self): | ||
"""Tests call to wait for an update with default parameters""" | ||
response = self.index.add_documents([{'id': 1, 'title': 'Le Petit Prince'}]) | ||
assert 'updateId' in response | ||
wait_update = self.index.wait_for_pending_update(response['updateId']) | ||
assert isinstance(wait_update, object) | ||
assert 'status' in wait_update | ||
assert wait_update['status'] != 'enqueued' | ||
|
||
def test_wait_for_pending_update_timeout(self): | ||
"""Tests timeout risen by waiting for an update""" | ||
with pytest.raises(TimeoutError): | ||
self.index.wait_for_pending_update(2, timeout_in_ms=0) | ||
|
||
def test_wait_for_pending_update_interval(self): | ||
"""Tests call to wait for an update with custom interval""" | ||
dataset_file = open("datasets/small_movies.json", "r") | ||
dataset_json = json.loads(dataset_file.read()) | ||
response = self.index.add_documents(dataset_json) | ||
assert 'updateId' in response | ||
start_time = datetime.now() | ||
wait_update = self.index.wait_for_pending_update( | ||
response['updateId'], | ||
interval_in_ms=1000, | ||
timeout_in_ms=6000 | ||
) | ||
time_delta = datetime.now() - start_time | ||
assert isinstance(wait_update, object) | ||
assert 'status' in wait_update | ||
assert wait_update['status'] != 'enqueued' | ||
assert time_delta.seconds >= 1 |
Uh oh!
There was an error while loading. Please reload this page.