Skip to content

Commit

Permalink
fix: use lazy field to control collection update task
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Oct 11, 2024
1 parent 6e887a9 commit d23a5fd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
20 changes: 13 additions & 7 deletions openedx/core/djangoapps/content/search/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions openedx/core/djangoapps/content_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
from openedx_events.content_authoring.data import (
ContentLibraryData,
LibraryBlockData,
LibraryCollectionData,
)
from openedx_events.content_authoring.signals import (
CONTENT_LIBRARY_CREATED,
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
33 changes: 25 additions & 8 deletions openedx/core/djangoapps/content_libraries/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))

Expand All @@ -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,
)

0 comments on commit d23a5fd

Please sign in to comment.