Skip to content

Commit

Permalink
fix: avoid infinite recursion of openedx-event
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Feb 9, 2024
1 parent 5d340fd commit 0a0b857
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
8 changes: 7 additions & 1 deletion openedx_events/apps.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""
openedx_events Django application initialization.
"""
import logging

from django.apps import AppConfig
from django.conf import settings

from openedx_events.event_bus import get_producer
from openedx_events.exceptions import ProducerConfigurationError
from openedx_events.tooling import OpenEdxPublicSignal, load_all_signals
from openedx_events.tooling import OpenEdxPublicSignal, load_all_signals, SIGNAL_PROCESSED_FROM_EVENT_BUS

logger = logging.getLogger(__name__)

def general_signal_handler(sender, signal, **kwargs): # pylint: disable=unused-argument
"""
Expand All @@ -20,6 +22,10 @@ def general_signal_handler(sender, signal, **kwargs): # pylint: disable=unused-
# "topic_a": { "event_key_field": "my.key.field", "enabled": True },
# "topic_b": { "event_key_field": "my.key.field", "enabled": False }
# }"
if SIGNAL_PROCESSED_FROM_EVENT_BUS in kwargs and kwargs[SIGNAL_PROCESSED_FROM_EVENT_BUS] is True:
logger.info(f"Signal already processed on the Event Bus {signal.event_type}, skipping...")
return

event_data = {key: kwargs.get(key) for key in signal.init_data}

for topic in event_type_producer_configs.keys():
Expand Down
13 changes: 8 additions & 5 deletions openedx_events/tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"org.openedx.learning.course.notification.requested.v1",
]

SIGNAL_PROCESSED_FROM_EVENT_BUS = "from_event_bus"


class OpenEdxPublicSignal(Signal):
"""
Expand Down Expand Up @@ -113,7 +115,7 @@ def generate_signal_metadata(self, time=None):
time=time,
)

def _send_event_with_metadata(self, metadata, send_robust=True, **kwargs):
def _send_event_with_metadata(self, metadata, send_robust=True, from_event_bus=False, **kwargs):
"""
Send events to all connected receivers with the provided metadata.
Expand Down Expand Up @@ -161,6 +163,7 @@ def validate_sender():
validate_sender()

kwargs["metadata"] = metadata
kwargs[SIGNAL_PROCESSED_FROM_EVENT_BUS]= from_event_bus

if self._allow_send_event_failure or settings.DEBUG or not send_robust:
return super().send(sender=None, **kwargs)
Expand All @@ -172,7 +175,7 @@ def validate_sender():

return responses

def send_event(self, send_robust=True, time=None, **kwargs):
def send_event(self, send_robust=True, from_event_bus=False, time=None, **kwargs):
"""
Send events to all connected receivers.
Expand Down Expand Up @@ -210,10 +213,10 @@ def send_event(self, send_robust=True, time=None, **kwargs):
the event.
"""
metadata = self.generate_signal_metadata(time=time)
return self._send_event_with_metadata(metadata=metadata, send_robust=send_robust, **kwargs)
return self._send_event_with_metadata(metadata=metadata, send_robust=send_robust, from_event_bus=from_event_bus, **kwargs)

def send_event_with_custom_metadata(
self, metadata, /, *, send_robust=True, **kwargs
self, metadata, /, *, send_robust=True, from_event_bus=False, **kwargs
):
"""
Send events to all connected receivers using the provided metadata.
Expand All @@ -232,7 +235,7 @@ def send_event_with_custom_metadata(
See ``send_event`` docstring for more details.
"""
return self._send_event_with_metadata(metadata=metadata, send_robust=send_robust, **kwargs)
return self._send_event_with_metadata(metadata=metadata, send_robust=send_robust, from_event_bus=from_event_bus, **kwargs)

def send(self, sender, **kwargs): # pylint: disable=unused-argument
"""
Expand Down

0 comments on commit 0a0b857

Please sign in to comment.