From 955c1f2a7011b51b3a83456df265114cf4ae3171 Mon Sep 17 00:00:00 2001 From: Rebecca Graber Date: Mon, 15 May 2023 11:23:19 -0400 Subject: [PATCH] feat: make signal parameter optional in preparation for removal (#219) * feat: make signal parameter optional in preparation for removal --- CHANGELOG.rst | 6 ++++++ openedx_events/__init__.py | 2 +- openedx_events/event_bus/__init__.py | 7 ++++--- .../management/commands/consume_events.py | 15 +++++---------- .../tests/test_consume_events_command.py | 10 +++------- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 951123d7..81a3165c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,12 @@ Change Log Unreleased ---------- +[7.3.0] - 2023-05-15 +-------------------- +Changed +~~~~~~~ +* Made `signal` argument optional in consume_events in preparation for removal + [7.2.0] - 2023-05-03 -------------------- Changed diff --git a/openedx_events/__init__.py b/openedx_events/__init__.py index 1bf1b3e6..18e3a36c 100644 --- a/openedx_events/__init__.py +++ b/openedx_events/__init__.py @@ -5,4 +5,4 @@ more information about the project. """ -__version__ = "7.2.0" +__version__ = "7.3.0" diff --git a/openedx_events/event_bus/__init__.py b/openedx_events/event_bus/__init__.py index ceab4b8b..68b485fc 100644 --- a/openedx_events/event_bus/__init__.py +++ b/openedx_events/event_bus/__init__.py @@ -157,7 +157,9 @@ def consume_indefinitely(self) -> None: # an instance of EventBusConsumer, calls to the consumer will be ignored with a warning at startup. -def make_single_consumer(*, topic: str, group_id: str, signal: OpenEdxPublicSignal, **kwargs) -> EventBusConsumer: +def make_single_consumer(*, topic: str, group_id: str, + signal: OpenEdxPublicSignal = None, # pylint: disable=unused-argument + **kwargs) -> EventBusConsumer: """ Construct a consumer for a given topic, group, and signal. @@ -166,12 +168,11 @@ def make_single_consumer(*, topic: str, group_id: str, signal: OpenEdxPublicSign Arguments: topic: The event bus topic to consume from (without any environmental prefix) group_id: The consumer group to participate in - signal: Send consumed, decoded events to the receivers of this signal + signal: DEPRECATED This argument will be ignored. Signals will be determined by message headers """ options = { 'topic': topic, 'group_id': group_id, - 'signal': signal, **kwargs, } return _try_load( diff --git a/openedx_events/management/commands/consume_events.py b/openedx_events/management/commands/consume_events.py index f3fc9ac1..3eae9c9e 100644 --- a/openedx_events/management/commands/consume_events.py +++ b/openedx_events/management/commands/consume_events.py @@ -7,7 +7,7 @@ from django.core.management.base import BaseCommand from openedx_events.event_bus import make_single_consumer -from openedx_events.tooling import OpenEdxPublicSignal, load_all_signals +from openedx_events.tooling import load_all_signals logger = logging.getLogger(__name__) @@ -22,16 +22,13 @@ class Command(BaseCommand): Example:: - python manage.py consume_events -t user-login -g user-activity-service \ - -s org.openedx.learning.auth.session.login.completed.v1 + python manage.py consume_events -t user-login -g user-activity-service # send extra args, for example pass check_backlog flag to redis consumer - python manage.py cms consume_events -t user-login -g user-activity-service \ - -s org.openedx.learning.auth.session.login.completed.v1 --extra '{"check_backlog": true}' + python manage.py cms consume_events -t user-login -g user-activity-service --extra '{"check_backlog": true}' # send extra args, for example replay events from specific redis msg id. python manage.py cms consume_events -t user-login -g user-activity-service \ - -s org.openedx.learning.auth.session.login.completed.v1 \ --extra '{"last_read_msg_id": "1679676448892-0"}' """ @@ -54,8 +51,8 @@ def add_arguments(self, parser): parser.add_argument( '-s', '--signal', nargs=1, - required=True, - help='Type of signal to emit from consumed messages.' + required=False, + help='DEPRECATED This argument will be ignored. Signals will be determined by message headers' ) parser.add_argument( '--extra', @@ -73,11 +70,9 @@ def handle(self, *args, **options): # load additional arguments specific for the underlying implementation of event_bus. extra = json.loads(options.get('extra') or '{}') load_all_signals() - signal = OpenEdxPublicSignal.get_signal_by_type(options['signal'][0]) event_consumer = make_single_consumer( topic=options['topic'][0], group_id=options['group_id'][0], - signal=signal, **extra, ) event_consumer.consume_indefinitely() diff --git a/openedx_events/tests/test_consume_events_command.py b/openedx_events/tests/test_consume_events_command.py index 66b77b67..f404c95c 100644 --- a/openedx_events/tests/test_consume_events_command.py +++ b/openedx_events/tests/test_consume_events_command.py @@ -23,12 +23,11 @@ def test_consumer_call_with_only_required_args(self, mock_make_consumer, _): Expected behavior: The required args are passed to consumer. """ - call_command(Command(), topic=['test'], group_id=['test'], signal=['test-signal']) - mock_make_consumer.assert_called_once_with(topic='test', group_id='test', signal='test-signal') + call_command(Command(), topic=['test'], group_id=['test']) + mock_make_consumer.assert_called_once_with(topic='test', group_id='test') - @patch('openedx_events.tooling.OpenEdxPublicSignal.get_signal_by_type', return_value="test-signal") @patch('openedx_events.management.commands.consume_events.make_single_consumer', autospec=True) - def test_consumer_call_with_extra_args(self, mock_make_consumer, _): + def test_consumer_call_with_extra_args(self, mock_make_consumer): """ This methods checks the extra args are correctly parsed and passed to consumer. @@ -39,13 +38,11 @@ def test_consumer_call_with_extra_args(self, mock_make_consumer, _): Command(), topic=['test'], group_id=['test'], - signal=['test-signal'], extra='{"check_backlog":true}' ) mock_make_consumer.assert_called_once_with( topic='test', group_id='test', - signal='test-signal', check_backlog=True ) @@ -62,7 +59,6 @@ def test_consumer_call_with_incorrect_json_string(self, mock_make_consumer, mock Command(), topic=['test'], group_id=['test'], - signal=['test-signal'], extra="{'check_backlog':true}" ) mock_logger.exception.assert_called_once_with("Error consuming events")