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 33cd7d5 commit ac0b58a
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 @@ -30,6 +30,8 @@
"org.openedx.learning.course.notification.requested.v1",
]

SIGNAL_PROCESSED_FROM_EVENT_BUS = "from_event_bus"


class OpenEdxPublicSignal(Signal):
"""
Expand Down Expand Up @@ -115,7 +117,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 @@ -163,6 +165,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 @@ -174,7 +177,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 @@ -212,10 +215,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 @@ -234,7 +237,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 ac0b58a

Please sign in to comment.