Skip to content

Wait for Asynchronous Tasks

Clémentine Urquizar - curqui edited this page Jan 27, 2022 · 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).

More about the asynchronous tasks of Meilisearch

You might need to wait for the complete execution of this task before executing the rest of your code.

🚨 The following methods should not be used in production!

wait_for_task

The wait_for_task method makes the task synchronous:

def wait_for_task(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_task:

puts 'Adding Le Petit Prince...'
response = index.add_document(id: 1, title: 'Le Petit Prince')
task_uid = response['uid']
wait_for_task(task_uid)
puts 'Addition done!'
Adding Le Petit Prince...
[Meilisearch has finished adding the document]
Addition done!

Without wait_for_task:

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]

Bang (!) methods

This library provides the following synchronous methods:

  • add_documents!
  • update_documents!
  • delete_documents!
  • delete_document!
  • delete_all_documents!

These methods stop your code execution until the asynchronous task has been processed by Meilisearch.

Clone this wiki locally