Skip to content

Commit

Permalink
fixup!: changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Graber committed Oct 5, 2023
1 parent 36ba5dc commit c82fc3b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Unreleased

[9.0.0] - 2023-10-04
--------------------
Changed
~~~~~~~
* Re-licensed this repository from AGPL 3.0 to Apache 2.0
* **Breaking change**: Restructured EVENT_BUS_PRODUCER_CONFIG

Expand Down
14 changes: 9 additions & 5 deletions openedx_events/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ def general_signal_handler(sender, signal, **kwargs): # pylint: disable=unused-
"""
Signal handler for publishing events to configured event bus.
"""
configurations = getattr(settings, "EVENT_BUS_PRODUCER_CONFIG", {}).get(signal.event_type, {})
event_type_publish_configs = getattr(settings, "EVENT_BUS_PRODUCER_CONFIG", {}).get(signal.event_type, {})
# event_type_publish_configs should look something like
# {
# "topic_a": { "event_key_field": "my.key.field", "enabled": True },
# "topic_b": { "event_key_field": "my.key.field", "enabled": False }
# }"
event_data = {key: kwargs.get(key) for key in signal.init_data}

for topic in configurations.keys():
if configurations[topic]["enabled"]:
for topic in event_type_publish_configs.keys():
if event_type_publish_configs[topic]["enabled"] is True:
get_producer().send(
signal=signal,
topic=topic,
event_key_field=configurations[topic]["event_key_field"],
event_key_field=event_type_publish_configs[topic]["event_key_field"],
event_data=event_data,
event_metadata=kwargs["metadata"],
)
Expand Down Expand Up @@ -58,7 +63,6 @@ def _get_validated_signal_config(self, event_type, configuration):
except KeyError as exc:
raise ProducerConfigurationError(message=f"No OpenEdxPublicSignal of type: '{event_type}'.") from exc
for _, topic_configuration in configuration.items():
print(f"{topic_configuration=}")
if not isinstance(topic_configuration, dict):
raise ProducerConfigurationError(
event_type=event_type,
Expand Down
11 changes: 6 additions & 5 deletions openedx_events/event_bus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
``EVENT_BUS_CONSUMER``.
"""

import copy
import warnings
from abc import ABC, abstractmethod
from functools import lru_cache
Expand Down Expand Up @@ -185,21 +186,21 @@ def _reset_state(sender, **kwargs): # pylint: disable=unused-argument
get_producer.cache_clear()


def merge_publisher_configs(publisher_config_a, publisher_config_b):
def merge_publisher_configs(publisher_config_original, publisher_config_overrides):
"""
Merge two EVENT_BUS_PRODUCER_CONFIG maps.
Arguments:
publisher_config_a: An EVENT_BUS_PRODUCER_CONFIG-structured map
publisher_config_b: An EVENT_BUS_PRODUCER_CONFIG-structured map
publisher_config_original: An EVENT_BUS_PRODUCER_CONFIG-structured map
publisher_config_overrides: An EVENT_BUS_PRODUCER_CONFIG-structured map
Returns:
A new EVENT_BUS_PRODUCER_CONFIG map created by combining the two maps. All event_type/topic pairs in
publisher_config_b are added to the publisher_config_a. If there is a conflict on whether a particular
event_type/topic pair is enabled, publisher_config_b wins out.
"""
combined = {**publisher_config_a}
for event_type, event_type_config_b in publisher_config_b.items():
combined = copy.deepcopy(publisher_config_original)
for event_type, event_type_config_b in publisher_config_overrides.items():
event_type_config_combined = combined.get(event_type, {})
for topic, topic_config_b in event_type_config_b.items():
topic_config_combined = event_type_config_combined.get(topic, {})
Expand Down
7 changes: 7 additions & 0 deletions openedx_events/event_bus/tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests for event bus implementation loader.
"""

import copy
import warnings
from contextlib import contextmanager
from unittest import TestCase
Expand Down Expand Up @@ -139,6 +140,8 @@ def test_merge_configs(self):
'topic_c': {'event_key_field': 'field', 'enabled': True},
}
}
# for ensuring we didn't change the original dict
dict_a_copy = copy.deepcopy(dict_a)
dict_b = {
'event_type_0': {
# disable an existing event/topic pairing
Expand All @@ -151,6 +154,7 @@ def test_merge_configs(self):
'topic_e': {'event_key_field': 'field', 'enabled': True},
}
}
dict_b_copy = copy.deepcopy(dict_b)
result = merge_publisher_configs(dict_a, dict_b)
self.assertDictEqual(result, {
'event_type_0': {
Expand All @@ -165,6 +169,8 @@ def test_merge_configs(self):
'topic_e': {'event_key_field': 'field', 'enabled': True},
}
})
self.assertDictEqual(dict_a, dict_a_copy)
self.assertDictEqual(dict_b, dict_b_copy)

def test_merge_configs_with_empty(self):
dict_a = {
Expand All @@ -179,3 +185,4 @@ def test_merge_configs_with_empty(self):
dict_b = {}
result = merge_publisher_configs(dict_a, dict_b)
self.assertDictEqual(result, dict_a)

6 changes: 3 additions & 3 deletions openedx_events/tests/test_producer_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def test_enabled_disabled_events(self, mock_producer):
# check that call_args_list only consists of enabled topics.
call_args = mock_send.send.call_args_list[0][1]
self.assertDictContainsSubset(
{'topic': 'content-authoring-xblock-lifecycle', 'event_key_field': 'xblock_info.usage_key'},
{'topic': 'enabled_topic_a', 'event_key_field': 'xblock_info.usage_key'},
call_args
)
call_args = mock_send.send.call_args_list[1][1]
self.assertDictContainsSubset(
{'topic': 'content-authoring-all-status', 'event_key_field': 'xblock_info.usage_key'},
{'topic': 'enabled_topic_b', 'event_key_field': 'xblock_info.usage_key'},
call_args
)

Expand Down Expand Up @@ -94,7 +94,7 @@ def test_configuration_is_validated(self):

with override_settings(
EVENT_BUS_PRODUCER_CONFIG={
"org.openedx.content_authoring.xblock.deleted.v1": {"some": {"enabled": True}}
"org.openedx.content_authoring.xblock.deleted.v1": {"topic": {"enabled": True}}
}
):
with pytest.raises(ProducerConfigurationError, match="missing 'event_key_field' key."):
Expand Down
6 changes: 3 additions & 3 deletions test_utils/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
EVENT_BUS_PRODUCER_CONFIG = {
'org.openedx.content_authoring.xblock.published.v1':
{
'content-authoring-xblock-lifecycle': {'event_key_field': 'xblock_info.usage_key', 'enabled': True},
'content-authoring-all-status': {'event_key_field': 'xblock_info.usage_key', 'enabled': True},
'content-authoring-xblock-published': {'event_key_field': 'xblock_info.usage_key', 'enabled': False}
'enabled_topic_a': {'event_key_field': 'xblock_info.usage_key', 'enabled': True},
'enabled_topic_b': {'event_key_field': 'xblock_info.usage_key', 'enabled': True},
'disabled_topic': {'event_key_field': 'xblock_info.usage_key', 'enabled': False}
},
'org.openedx.content_authoring.xblock.deleted.v1':
{
Expand Down

0 comments on commit c82fc3b

Please sign in to comment.