2121import logging
2222import os
2323import shutil
24+ from collections .abc import Iterable
2425from datetime import datetime
2526from io import StringIO
2627from tempfile import NamedTemporaryFile , mkdtemp
@@ -204,14 +205,16 @@ def check_container_content_changes(
204205 else :
205206 old_entity_list_id = None
206207 if new_version_id :
207- new_version = content_api .get_container_version (new_version_id ) if new_version_id else None
208+ new_version = content_api .get_container_version (new_version_id )
208209 new_entity_list_id = new_version .entity_list_id
209210 else :
210211 new_entity_list_id = None
211212
213+ old_child_ids : Iterable [PublishableEntity .ID ]
214+ new_child_ids : Iterable [PublishableEntity .ID ]
212215 # If the title has changed, we notify ALL children that their parent container(s) have changed, e.g. to update the
213216 # list of "units this component is used in", "sections this subsection is used in", etc. in the search index
214- title_changed : bool = old_version and new_version and old_version .title != new_version .title
217+ title_changed : bool = bool ( old_version and new_version ) and ( old_version .title != new_version .title )
215218 if title_changed :
216219 # TODO: there is no "get entity list for container version" API in openedx_content
217220 new_child_ids = new_version .entity_list .entitylistrow_set .values_list ("entity_id" , flat = True )
@@ -297,7 +300,7 @@ def send_collections_changed_events(
297300
298301@shared_task (base = LoggedTask )
299302@set_code_owner_attribute
300- def send_events_after_publish (publish_log_pk : int , library_key_str : str ) -> None :
303+ def send_events_after_publish (publish_log_id : int , library_key_str : str ) -> None :
301304 """
302305 Send events to trigger actions like updating the search index, after we've
303306 published some items in a library.
@@ -311,12 +314,11 @@ def send_events_after_publish(publish_log_pk: int, library_key_str: str) -> None
311314 event handlers like updating the search index may a while to complete in
312315 that case.
313316 """
314- publish_log = PublishLog .objects .get (pk = publish_log_pk )
317+ publish_log = PublishLog .objects .get (id = publish_log_id )
315318 library_key = LibraryLocatorV2 .from_string (library_key_str )
316319 affected_entities = publish_log .records .select_related (
317320 "entity" , "entity__container" , "entity__container__container_type" , "entity__component" ,
318321 ).all ()
319- affected_containers : set [LibraryContainerLocator ] = set ()
320322
321323 # Update anything that needs to be updated (e.g. search index):
322324 for record in affected_entities :
@@ -330,61 +332,21 @@ def send_events_after_publish(publish_log_pk: int, library_key_str: str) -> None
330332 LIBRARY_BLOCK_PUBLISHED .send_event (
331333 library_block = LibraryBlockData (library_key = library_key , usage_key = usage_key )
332334 )
333- # Publishing a container will auto-publish its children, but publishing a single component or all changes
334- # in the library will NOT usually include any parent containers. But we do need to notify listeners that the
335- # parent container(s) have changed, e.g. so the search index can update the "has_unpublished_changes"
336- try :
337- for parent_container in api .get_containers_contains_item (usage_key ):
338- affected_containers .add (parent_container .container_key )
339- # TODO: should this be a CONTAINER_CHILD_PUBLISHED event instead of CONTAINER_PUBLISHED ?
340- except api .ContentLibraryBlockNotFound :
341- # The component has been deleted.
342- pass
343335 elif hasattr (record .entity , "container" ):
344336 container_key = api .library_container_locator (library_key , record .entity .container )
345- affected_containers .add (container_key )
346-
347- try :
348- # We do need to notify listeners that the parent container(s) have changed,
349- # e.g. so the search index can update the "has_unpublished_changes"
350- for parent_container in api .get_containers_contains_item (container_key ):
351- affected_containers .add (parent_container .container_key )
352- except api .ContentLibraryContainerNotFound :
353- # The deleted children remains in the entity, so, in this case, the container may not be found.
354- pass
337+ # Note: this container may have been directly published, or perhaps one of its children was published and
338+ # it hasn't technically changed. Such ancestors of published entities are still included in the publish log.
339+ # .. event_implemented_name: LIBRARY_CONTAINER_PUBLISHED
340+ # .. event_type: org.openedx.content_authoring.content_library.container.published.v1
341+ LIBRARY_CONTAINER_PUBLISHED .send_event (
342+ library_container = LibraryContainerData (container_key = container_key )
343+ )
355344 else :
356345 log .warning (
357346 f"PublishableEntity { record .entity .pk } / { record .entity .entity_ref } "
358347 "was modified during publish operation but is of unknown type."
359348 )
360349
361- for container_key in affected_containers :
362- # .. event_implemented_name: LIBRARY_CONTAINER_PUBLISHED
363- # .. event_type: org.openedx.content_authoring.content_library.container.published.v1
364- LIBRARY_CONTAINER_PUBLISHED .send_event (
365- library_container = LibraryContainerData (container_key = container_key )
366- )
367-
368-
369- def wait_for_post_publish_events (publish_log : PublishLog , library_key : LibraryLocatorV2 ):
370- """
371- After publishing some changes, trigger the required event handlers (e.g.
372- update the search index). Try to wait for that to complete before returning,
373- up to some reasonable timeout, and then finish anything remaining
374- asynchonrously.
375- """
376- # Update the search index (and anything else) for the affected blocks
377- result = send_events_after_publish .apply_async (args = (publish_log .pk , str (library_key )))
378- # Try waiting a bit for those post-publish events to be handled:
379- try :
380- result .get (timeout = 15 )
381- except TimeoutError :
382- pass
383- # This is fine! The search index is still being updated, and/or other
384- # event handlers are still following up on the results, but the publish
385- # already *did* succeed, and the events will continue to be processed in
386- # the background by the celery worker until everything is updated.
387-
388350
389351def _filter_child (store , usage_key , capa_type ):
390352 """
0 commit comments