Skip to content

Commit

Permalink
feat: make signal parameter optional in preparation for removal (#219)
Browse files Browse the repository at this point in the history
* feat: make signal parameter optional in preparation for removal
  • Loading branch information
Rebecca Graber authored May 15, 2023
1 parent c3deb78 commit 955c1f2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion openedx_events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
more information about the project.
"""

__version__ = "7.2.0"
__version__ = "7.3.0"
7 changes: 4 additions & 3 deletions openedx_events/event_bus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down
15 changes: 5 additions & 10 deletions openedx_events/management/commands/consume_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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"}'
"""

Expand All @@ -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',
Expand All @@ -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()
Expand Down
10 changes: 3 additions & 7 deletions openedx_events/tests/test_consume_events_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
)

Expand All @@ -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")
Expand Down

0 comments on commit 955c1f2

Please sign in to comment.