diff --git a/openedx/core/djangoapps/content/search/handlers.py b/openedx/core/djangoapps/content/search/handlers.py index 085387d336b1..46b015367a0d 100644 --- a/openedx/core/djangoapps/content/search/handlers.py +++ b/openedx/core/djangoapps/content/search/handlers.py @@ -179,13 +179,19 @@ def library_collection_updated_handler(**kwargs) -> None: log.error("Received null or incorrect data for event") return - # Update collection index synchronously to make sure that search index is updated before - # the frontend invalidates/refetches index. - # See content_library_updated_handler for more details. - update_library_collection_index_doc.apply(args=[ - str(library_collection.library_key), - library_collection.collection_key, - ]) + if library_collection.lazy: + update_library_collection_index_doc.delay( + str(library_collection.library_key), + library_collection.collection_key, + ) + else: + # Update collection index synchronously to make sure that search index is updated before + # the frontend invalidates/refetches index. + # See content_library_updated_handler for more details. + update_library_collection_index_doc.apply(args=[ + str(library_collection.library_key), + library_collection.collection_key, + ]) @receiver(CONTENT_OBJECT_ASSOCIATIONS_CHANGED) diff --git a/openedx/core/djangoapps/content_libraries/api.py b/openedx/core/djangoapps/content_libraries/api.py index 07d07222234f..1ba00ac7122c 100644 --- a/openedx/core/djangoapps/content_libraries/api.py +++ b/openedx/core/djangoapps/content_libraries/api.py @@ -80,6 +80,7 @@ from openedx_events.content_authoring.data import ( ContentLibraryData, LibraryBlockData, + LibraryCollectionData, ) from openedx_events.content_authoring.signals import ( CONTENT_LIBRARY_CREATED, @@ -88,6 +89,7 @@ LIBRARY_BLOCK_CREATED, LIBRARY_BLOCK_DELETED, LIBRARY_BLOCK_UPDATED, + LIBRARY_COLLECTION_UPDATED, ) from openedx_learning.api import authoring as authoring_api from openedx_learning.api.authoring_models import Collection, Component, MediaType, LearningPackage, PublishableEntity @@ -1294,9 +1296,16 @@ def set_library_component_collections( created_by=created_by, ) - from ..content.search.tasks import update_library_collection_index_doc + # For each collection, trigger LIBRARY_COLLECTION_UPDATED signal and set lazy=True to trigger + # collection indexing asynchronously. for collection in affected_collections: - update_library_collection_index_doc.delay(str(library_key), collection.key) + LIBRARY_COLLECTION_UPDATED.send_event( + library_collection=LibraryCollectionData( + library_key=library_key, + collection_key=collection.key, + lazy=True, + ) + ) return component diff --git a/openedx/core/djangoapps/content_libraries/tests/test_api.py b/openedx/core/djangoapps/content_libraries/tests/test_api.py index 9ab60a2c9d26..4e49bff8c9f1 100644 --- a/openedx/core/djangoapps/content_libraries/tests/test_api.py +++ b/openedx/core/djangoapps/content_libraries/tests/test_api.py @@ -510,10 +510,11 @@ def test_update_collection_components_from_wrong_library(self): ) assert self.lib1_problem_block["id"] in str(exc.exception) - @mock.patch('openedx.core.djangoapps.content.search.api.upsert_library_collection_index_doc') - def test_set_library_component_collections(self, mock_update_collection_index_doc): + def test_set_library_component_collections(self): event_receiver = mock.Mock() CONTENT_OBJECT_ASSOCIATIONS_CHANGED.connect(event_receiver) + collection_update_event_receiver = mock.Mock() + LIBRARY_COLLECTION_UPDATED.connect(collection_update_event_receiver) assert not list(self.col2.entities.all()) component = api.get_component_from_usage_key(UsageKey.from_string(self.lib2_problem_block["id"])) @@ -536,11 +537,27 @@ def test_set_library_component_collections(self, mock_update_collection_index_do }, event_receiver.call_args_list[0].kwargs, ) - self.assertListEqual( - list(mock_update_collection_index_doc.call_args_list[0][0]), - [self.lib2.library_key, self.col2.key] + self.assertDictContainsSubset( + { + "signal": LIBRARY_COLLECTION_UPDATED, + "sender": None, + "library_collection": LibraryCollectionData( + self.lib2.library_key, + collection_key=self.col2.key, + lazy=True, + ), + }, + collection_update_event_receiver.call_args_list[0].kwargs, ) - self.assertListEqual( - list(mock_update_collection_index_doc.call_args_list[1][0]), - [self.lib2.library_key, self.col3.key] + self.assertDictContainsSubset( + { + "signal": LIBRARY_COLLECTION_UPDATED, + "sender": None, + "library_collection": LibraryCollectionData( + self.lib2.library_key, + collection_key=self.col3.key, + lazy=True, + ), + }, + collection_update_event_receiver.call_args_list[1].kwargs, )