Skip to content

Commit e844b20

Browse files
feat: more sophisticated handling of sync-vs-async for library edits
1 parent 9ce338a commit e844b20

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

openedx/core/djangoapps/content_libraries/signal_handlers.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import logging
6+
from functools import partial
67

78
from attrs import asdict
89
from django.dispatch import receiver
@@ -45,15 +46,26 @@ def entities_updated(
4546
except ContentLibrary.DoesNotExist:
4647
return # We don't care about non-library events.
4748

48-
entities_changed = [change.entity_id for change in change_log.changes]
49+
# Which entities were _directly_ changed here?
50+
direct_changes = [asdict(change) for change in change_log.changes if change.new_version != change.old_version]
51+
# And which entities were indirectly affected (e.g. parent containers)?
52+
indirect_changes = [asdict(change) for change in change_log.changes if change.new_version == change.old_version]
4953

50-
if len(entities_changed) == 1:
51-
fn = tasks.send_change_events_for_modified_entities
54+
update_task_fn = tasks.send_change_events_for_modified_entities
55+
update_sync = partial(update_task_fn, learning_package_id=learning_package.id)
56+
update_async = partial(update_task_fn.delay, learning_package_id=learning_package.id)
57+
58+
if len(direct_changes) == 1:
59+
# We directly changed only one entity. Update it synchronously so that the UI will reflect changes right away.
60+
if len(indirect_changes) <= 1:
61+
# And update any other affected entity synchronously too; there's at most one. (More efficient, better UX.)
62+
update_sync(change_list=[*direct_changes, *indirect_changes])
63+
else:
64+
update_sync(change_list=direct_changes) # Update this one entity synchronously, and
65+
update_async(change_list=indirect_changes) # update the many other affects entities async.
5266
else:
5367
# More than one entity was changed at once. Handle asynchronously:
54-
fn = tasks.send_change_events_for_modified_entities.delay
55-
56-
fn(learning_package_id=learning_package.id, change_list=[asdict(chg) for chg in change_log.changes])
68+
update_async(change_list=[*direct_changes, *indirect_changes])
5769

5870

5971
@receiver(content_signals.LEARNING_PACKAGE_ENTITIES_PUBLISHED)

0 commit comments

Comments
 (0)