Skip to content

Implement wait_for_pending_update method #81

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

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion meilisearch/index.py
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
Expand Down Expand Up @@ -170,7 +172,7 @@ def get_update_status(self, update_id):
Returns
----------
update: `list`
List of all enqueued and processed actions of the index.
List containing the details of the update status.
"""
return self.http.get(
'{}/{}/{}/{}'.format(
Expand All @@ -181,6 +183,33 @@ def get_update_status(self, update_id):
)
)

def wait_for_pending_update(self, update_id, timeout_in_ms=5000, interval_in_ms=50):
"""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 method should wait before rising a TimeoutError
interval_in_ms (optional): int
time interval the method should wait (sleep) between requests
Returns
----------
update: `dict`
Dictionary containing the details of the processed update status.
"""
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)
time_delta = datetime.now() - start_time
elapsed_time = time_delta.seconds * 1000 + time_delta.microseconds / 1000
raise TimeoutError

def get_stats(self):
"""Get stats of an index

Expand Down
61 changes: 61 additions & 0 deletions meilisearch/tests/test_wait_for_pending_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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
dataset_file = open("datasets/small_movies.json", "r")
dataset_json = None

def setup_class(self):
self.index = self.client.create_index(uid='indexUID')
self.dataset_json = json.loads(self.dataset_file.read())

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_custom(self):
"""Tests call to wait for an update with custom interval"""
response = self.index.add_documents(self.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

def test_wait_for_pending_update_interval_zero(self):
"""Tests call to wait for an update with custom interval"""
response = self.index.add_documents(self.dataset_json)
assert 'updateId' in response
wait_update = self.index.wait_for_pending_update(
response['updateId'],
interval_in_ms=0,
timeout_in_ms=6000
)
assert isinstance(wait_update, object)
assert 'status' in wait_update
assert wait_update['status'] != 'enqueued'