@@ -152,11 +152,21 @@ def _wait_for_meili_task(info: TaskInfo) -> None:
152152 Simple helper method to wait for a Meilisearch task to complete
153153 This method will block until the task is completed, so it should only be used in celery tasks
154154 or management commands.
155+
156+ ✨ Note: "Meilisearch processes tasks in the order they were added to the queue."
157+ per https://www.meilisearch.com/docs/capabilities/indexing/tasks_and_batches/monitor_tasks#monitoring-task-status
158+ so if you need to wait for multiple tasks, simply wait for the final (last) task.
155159 """
156160 client = _get_meilisearch_client ()
161+ # This function almost always gets called immediately after enqueing a task, and from experiments, an initial wait
162+ # of at least 15ms is warranted, as the task is almost never done in less than 10ms. We are using 20ms which seems
163+ # to work well without requiring an additional wait in most cases.
164+ sleep_delay = 0.020 # Initial wait is only 20ms but we will back off exponentially
165+ time .sleep (sleep_delay )
157166 current_status = client .get_task (info .task_uid )
158167 while current_status .status in ("enqueued" , "processing" ):
159- time .sleep (0.5 )
168+ time .sleep (sleep_delay )
169+ sleep_delay = min (sleep_delay * 1.5 , 2.0 ) # Increase delay up to 2s
160170 current_status = client .get_task (info .task_uid )
161171 if current_status .status != "succeeded" :
162172 try :
@@ -166,15 +176,6 @@ def _wait_for_meili_task(info: TaskInfo) -> None:
166176 raise MeilisearchError (err_reason )
167177
168178
169- def _wait_for_meili_tasks (info_list : list [TaskInfo ]) -> None :
170- """
171- Simple helper method to wait for multiple Meilisearch tasks to complete
172- """
173- while info_list :
174- info = info_list .pop ()
175- _wait_for_meili_task (info )
176-
177-
178179def _index_exists (index_name : str ) -> bool :
179180 """
180181 Check if an index exists
@@ -324,13 +325,10 @@ def _update_index_docs(docs) -> None:
324325 client = _get_meilisearch_client ()
325326 current_rebuild_index_name = _get_running_rebuild_index_name ()
326327
327- tasks = []
328328 if current_rebuild_index_name :
329329 # If there is a rebuild in progress, the document will also be added to the new index.
330- tasks .append (client .index (current_rebuild_index_name ).update_documents (docs ))
331- tasks .append (client .index (STUDIO_INDEX_NAME ).update_documents (docs ))
332-
333- _wait_for_meili_tasks (tasks )
330+ client .index (current_rebuild_index_name ).update_documents (docs )
331+ _wait_for_meili_task (client .index (STUDIO_INDEX_NAME ).update_documents (docs ))
334332
335333
336334def only_if_meilisearch_enabled (f ):
@@ -828,13 +826,10 @@ def _delete_documents(filter_query: str) -> None:
828826 client = _get_meilisearch_client ()
829827 current_rebuild_index_name = _get_running_rebuild_index_name ()
830828
831- tasks = []
832829 if current_rebuild_index_name :
833830 # If there is a rebuild in progress, the document will also be removed from the new index.
834- tasks .append (client .index (current_rebuild_index_name ).delete_documents (filter = filter_query ))
835- tasks .append (client .index (STUDIO_INDEX_NAME ).delete_documents (filter = filter_query ))
836-
837- _wait_for_meili_tasks (tasks )
831+ client .index (current_rebuild_index_name ).delete_documents (filter = filter_query )
832+ _wait_for_meili_task (client .index (STUDIO_INDEX_NAME ).delete_documents (filter = filter_query ))
838833
839834
840835def _delete_index_doc (doc_id ) -> None :
@@ -849,14 +844,11 @@ def _delete_index_doc(doc_id) -> None:
849844 client = _get_meilisearch_client ()
850845 current_rebuild_index_name = _get_running_rebuild_index_name ()
851846
852- tasks = []
853847 if current_rebuild_index_name :
854848 # If there is a rebuild in progress, the document will also be removed from the new index.
855- tasks .append (client .index (current_rebuild_index_name ).delete_document (doc_id ))
856-
857- tasks .append (client .index (STUDIO_INDEX_NAME ).delete_document (doc_id ))
849+ client .index (current_rebuild_index_name ).delete_document (doc_id )
858850
859- _wait_for_meili_tasks ( tasks )
851+ _wait_for_meili_task ( client . index ( STUDIO_INDEX_NAME ). delete_document ( doc_id ) )
860852
861853
862854def upsert_library_block_index_doc (usage_key : UsageKey ) -> None :
0 commit comments