feat: update content libraries API to use events from openedx-core [FC-0117]#38437
feat: update content libraries API to use events from openedx-core [FC-0117]#38437bradenmacdonald wants to merge 9 commits intoopenedx:masterfrom
Conversation
|
Thanks for the pull request, @bradenmacdonald! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. DetailsWhere can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
| { # Not 100% sure we want this, but a PUBLISHED event is emitted for container 2 | ||
| # because one of its children's published versions has changed, so whether or | ||
| # not it contains unpublished changes may have changed and the search index | ||
| # may need to be updated. It is not actually published though. | ||
| # TODO: should this be a CONTAINER_CHILD_PUBLISHED event? | ||
| # No PUBLISHED event is emitted for container 2, because it doesn't have a published version yet. | ||
| # Publishing 'html_block' would have potentially affected it if container 2's published version had a | ||
| # reference to 'html_block', but it doesn't yet until we publish it. | ||
| ) | ||
|
|
||
| # note that container 2 is still unpublished | ||
| c2_after = self._get_container(container2["id"]) | ||
| assert c2_after["has_unpublished_changes"] | ||
|
|
||
| # publish container2 now: | ||
| self._publish_container(container2["id"]) | ||
| self.expect_new_events( | ||
| { # An event for container 1 being published: | ||
| "signal": LIBRARY_CONTAINER_PUBLISHED, | ||
| "library_container": LibraryContainerData( | ||
| container_key=LibraryContainerLocator.from_string(container2["id"]), | ||
| ), | ||
| }, | ||
| { # An event for the html block in container 2 only: | ||
| "signal": LIBRARY_BLOCK_PUBLISHED, | ||
| "library_block": LibraryBlockData( | ||
| self.lib1_key, LibraryUsageLocatorV2.from_string(html_block2["id"]), | ||
| ), | ||
| }, |
There was a problem hiding this comment.
It's a little hard to tell from the diff here (because of how it's split up), but before this PR, a spurious PUBLISHED event was emitted for container 2 before it was ever published at all. I think the new behavior is much more correct, because it's built on Learning Core's new publish log side effects. I have explained why in the test case and added additional tests to ensure side effects are still resulting in PUBLISHED events when they should be. (Once we actually published container 2)
| { | ||
| "signal": CONTENT_OBJECT_ASSOCIATIONS_CHANGED, | ||
| "content_object": ContentObjectChangedData( | ||
| object_id=str(container_key), | ||
| changes=["collections", "tags"], | ||
| ), | ||
| }, | ||
| # We used to emit CONTENT_OBJECT_ASSOCIATIONS_CHANGED here for the restored container, specifically noting | ||
| # that changes=["collections", "tags"], because deleted things may have collections+tags that are once | ||
| # again relevant when it is restored. However, the CREATED event should be sufficient for notifying of that. | ||
| # (Or should we emit CREATED+UPDATED to be extra sure?) |
There was a problem hiding this comment.
Flagging this, as it's a change - no longer emitting CONTENT_OBJECT_ASSOCIATIONS_CHANGED in the case of restoring a deleted object.
TODO: test publishing a thing with collections and tags, delete it, then "revert all changes" in the library UI and make sure it re-appears with collections and tags intact. I haven't tested this yet.
| # openedx_content also lists ancestor containers of the affected units as changed. | ||
| # We don't strictly need this at the moment, at least as far as keeping our search index updated. | ||
| { | ||
| "signal": LIBRARY_CONTAINER_UPDATED, | ||
| "library_container": LibraryContainerData(container_key=self.subsection1.container_key), | ||
| }, | ||
| { | ||
| "signal": LIBRARY_CONTAINER_UPDATED, | ||
| "library_container": LibraryContainerData(container_key=self.subsection2.container_key), | ||
| }, | ||
| { | ||
| "signal": LIBRARY_CONTAINER_UPDATED, | ||
| "library_container": LibraryContainerData(container_key=self.section1.container_key), | ||
| }, | ||
| { | ||
| "signal": LIBRARY_CONTAINER_UPDATED, | ||
| "library_container": LibraryContainerData(container_key=self.section2.container_key), |
There was a problem hiding this comment.
Last change: we now emit events for ancestors of parent containers of modified entities, which we weren't doing before (before it was only one level - parent containers but not their ancestors in turn). I don't think we have a use case for this, but I am not sure if I could or should filter them out somehow, as the publish log treats direct ancestors (which we definitely care about and need events for) and their ancestors in turn exactly the same.
To avoid performance issues, in such cases where more than one ancestor is included in the event stream, the event for the directly modified entity is emitted synchronously but the indirect container events are emitted asynchronously. This seems to work well in the UI, making it update correctly/immediately when e.g. renaming something, but should still preserve performance even if you rename a component used in thousands of different containers.
Description
With openedx/openedx-core#543, openedx-core now emits events when changes happen within a Learning Package.
This PR updates the content libraries code and search code accordingly. The main benefit is that the search index now stays up to date regardless of which APIs are used. We don't need to "wrap" some low-level APIs in high-level APIs just to add events.
Note: The "Library Collections" code was already working fine because it used Django signals to watch for changes to the Collection-PublishableEntity many-to-many relationship, but it shouldn't have been so aware of the internals of
openedx_content.Supporting information
See openedx/openedx-core#462
Testing instructions
Coming soon
Deadline
Verawood
Other information
Depends on openedx/openedx-core#543 .
I wrote most of the code but used Claude Code for small bits and pieces.