Skip to content

Wait for Asynchronous Tasks

Clémentine Urquizar edited this page Jan 25, 2021 · 6 revisions

Some MeiliSearch operations are asynchronous, e.g. the document addition or the settings update. They will be put in a queue and will be executed in turn (asynchronously).

You might need to wait for the complete execution of this task before executing the rest of your code.
The wait_for_pending_update method makes the task synchronous:

def wait_for_pending_update(update_id, timeout_in_ms = 5000, interval_in_ms = 50)

⚠️ Don't use this method if you process an operation on a large batch of documents. It would stop your code execution for too long.

Example

With wait_for_pending_update:

puts 'Adding Le Petit Prince...'
response = index.add_document(id: 1, title: 'Le Petit Prince')
update_id = response['updateId']
wait_for_pending_update(update_id)
puts 'Addition done!'
Adding Le Petit Prince...
[MeiliSearch has finished adding the document]
Addition done!

Without wait_for_pending_update:

puts 'Adding Le Petit Prince...'
response = index.add_document(id: 1, title: 'Le Petit Prince')
puts 'Addition done!'
Adding Le Petit Prince...
Addition done!
[MeiliSearch has now finished adding the document, but not before the "Addition done!" was written]
Clone this wiki locally