From 0a0b857d330b5e0c8184f8266b304a94e7c6b7d0 Mon Sep 17 00:00:00 2001 From: Cristhian Garcia Date: Fri, 9 Feb 2024 13:21:33 -0500 Subject: [PATCH] fix: avoid infinite recursion of openedx-event --- openedx_events/apps.py | 8 +++++++- openedx_events/tooling.py | 13 ++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/openedx_events/apps.py b/openedx_events/apps.py index 6326e620..4b3d93cc 100644 --- a/openedx_events/apps.py +++ b/openedx_events/apps.py @@ -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 """ @@ -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(): diff --git a/openedx_events/tooling.py b/openedx_events/tooling.py index bee23a9a..4aba3013 100644 --- a/openedx_events/tooling.py +++ b/openedx_events/tooling.py @@ -28,6 +28,8 @@ "org.openedx.learning.course.notification.requested.v1", ] +SIGNAL_PROCESSED_FROM_EVENT_BUS = "from_event_bus" + class OpenEdxPublicSignal(Signal): """ @@ -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. @@ -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) @@ -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. @@ -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. @@ -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 """