From dc2d4b06ac870d68fe5ad8750fec62c3d7138645 Mon Sep 17 00:00:00 2001
From: Navin Karkera <navin@opencraft.com>
Date: Fri, 23 Aug 2024 17:13:30 +0530
Subject: [PATCH] fix: update library index synchronously

Discarded components need to be removed from index in sync to make sure
that users see the latest updated data.
---
 openedx/core/djangoapps/content/search/handlers.py | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/openedx/core/djangoapps/content/search/handlers.py b/openedx/core/djangoapps/content/search/handlers.py
index ba0e8c1a1680..94ac02ea1606 100644
--- a/openedx/core/djangoapps/content/search/handlers.py
+++ b/openedx/core/djangoapps/content/search/handlers.py
@@ -136,7 +136,13 @@ def content_library_updated_handler(**kwargs) -> None:
         log.error("Received null or incorrect data for event")
         return
 
-    update_content_library_index_docs.delay(str(content_library_data.library_key))
+    # Update content library index synchronously to make sure that search index is updated before
+    # the frontend invalidates/refetches index.
+    # Currently, this is only required to make sure that removed/discarded components are removed
+    # from the search index and displayed to user properly. If it becomes a performance bottleneck
+    # for other update operations other than discard, we can update CONTENT_LIBRARY_UPDATED event
+    # to include a parameter which can help us decide if the task needs to run sync or async.
+    update_content_library_index_docs.apply(args=[str(content_library_data.library_key)])
 
 
 @receiver(CONTENT_OBJECT_TAGS_CHANGED)