Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions optimizely/odp/odp_event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Signal(Enum):
"""Enum for sending signals to the event queue."""
SHUTDOWN = 1
FLUSH = 2
UPDATE_CONFIG = 3


class OdpEventManager:
Expand All @@ -56,7 +57,11 @@ def __init__(
"""
self.logger = logger or _logging.NoOpLogger()
self.zaius_manager = api_manager or ZaiusRestApiManager(self.logger)

self.odp_config = odp_config
self.api_key = odp_config.get_api_key()
self.api_host = odp_config.get_api_host()

self.event_queue: Queue[OdpEvent | Signal] = Queue(OdpEventManagerConfig.DEFAULT_QUEUE_CAPACITY)
self.batch_size = OdpEventManagerConfig.DEFAULT_BATCH_SIZE
self.flush_interval = OdpEventManagerConfig.DEFAULT_FLUSH_INTERVAL
Expand Down Expand Up @@ -102,7 +107,11 @@ def _run(self) -> None:
self.logger.debug('ODP event queue: received flush signal.')
self._flush_batch()
self.event_queue.task_done()
continue

elif item == Signal.UPDATE_CONFIG:
self.logger.debug('ODP event queue: received update config signal.')
self._update_config()
self.event_queue.task_done()

elif isinstance(item, OdpEvent):
self._add_to_batch(item)
Expand Down Expand Up @@ -137,10 +146,7 @@ def _flush_batch(self) -> None:
self.logger.debug('ODP event queue: nothing to flush.')
return

api_key = self.odp_config.get_api_key()
api_host = self.odp_config.get_api_host()

if not api_key or not api_host:
if not self.api_key or not self.api_host:
self.logger.debug(Errors.ODP_NOT_INTEGRATED)
self._current_batch.clear()
return
Expand All @@ -150,7 +156,7 @@ def _flush_batch(self) -> None:

for i in range(1 + self.retry_count):
try:
should_retry = self.zaius_manager.send_odp_events(api_key, api_host, self._current_batch)
should_retry = self.zaius_manager.send_odp_events(self.api_key, self.api_host, self._current_batch)
except Exception as error:
should_retry = False
self.logger.error(Errors.ODP_EVENT_FAILED.format(f'Error: {error} {self._current_batch}'))
Expand Down Expand Up @@ -241,3 +247,18 @@ def dispatch(self, event: OdpEvent) -> None:
def identify_user(self, user_id: str) -> None:
self.send_event(OdpManagerConfig.EVENT_TYPE, 'identified',
{OdpManagerConfig.KEY_FOR_USER_ID: user_id}, {})

def update_config(self) -> None:
"""Adds update config signal to event_queue."""
try:
self.event_queue.put_nowait(Signal.UPDATE_CONFIG)
except Full:
self.logger.error("Error updating ODP config for the event queue")

def _update_config(self) -> None:
"""Updates the configuration used to send events."""
if len(self._current_batch) > 0:
self._flush_batch()

self.api_host = self.odp_config.get_api_host()
self.api_key = self.odp_config.get_api_key()
17 changes: 13 additions & 4 deletions tests/test_odp_event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ def test_odp_event_manager_events_before_odp_ready(self, *args):
event_manager.send_event(**self.events[1])

odp_config.update(self.api_key, self.api_host, [])
event_manager.update_config()
event_manager.event_queue.join()

event_manager.send_event(**self.events[0])
Expand All @@ -423,6 +424,7 @@ def test_odp_event_manager_events_before_odp_ready(self, *args):
mock_logger.debug.assert_has_calls([
mock.call('ODP event queue: cannot send before the datafile has loaded.'),
mock.call('ODP event queue: cannot send before the datafile has loaded.'),
mock.call('ODP event queue: received update config signal.'),
mock.call('ODP event queue: adding event.'),
mock.call('ODP event queue: adding event.'),
mock.call('ODP event queue: received flush signal.'),
Expand All @@ -442,6 +444,7 @@ def test_odp_event_manager_events_before_odp_disabled(self, *args):
event_manager.send_event(**self.events[1])

odp_config.update(None, None, [])
event_manager.update_config()
event_manager.event_queue.join()

event_manager.send_event(**self.events[0])
Expand All @@ -453,6 +456,7 @@ def test_odp_event_manager_events_before_odp_disabled(self, *args):
mock_logger.debug.assert_has_calls([
mock.call('ODP event queue: cannot send before the datafile has loaded.'),
mock.call('ODP event queue: cannot send before the datafile has loaded.'),
mock.call('ODP event queue: received update config signal.'),
mock.call(Errors.ODP_NOT_INTEGRATED),
mock.call(Errors.ODP_NOT_INTEGRATED)
])
Expand Down Expand Up @@ -496,20 +500,25 @@ def test_odp_event_manager_disabled_after_events_in_queue(self, *args):
odp_config = OdpConfig(self.api_key, self.api_host)

event_manager = OdpEventManager(odp_config, mock_logger)
event_manager.batch_size = 2
event_manager.batch_size = 3

with mock.patch('optimizely.odp.odp_event_manager.OdpEventManager.is_running', True):
event_manager.send_event(**self.events[0])
event_manager.send_event(**self.events[1])

with mock.patch.object(event_manager.zaius_manager, 'send_odp_events') as mock_send:
odp_config.update(None, None, [])
event_manager.update_config()

with mock.patch.object(
event_manager.zaius_manager, 'send_odp_events', new_callable=CopyingMock, return_value=False
) as mock_send:
event_manager.start()
event_manager.send_event(**self.events[0])
event_manager.send_event(**self.events[1])
event_manager.send_event(**self.events[0])
event_manager.event_queue.join()

self.assertEqual(len(event_manager._current_batch), 0)
mock_logger.debug.assert_any_call(Errors.ODP_NOT_INTEGRATED)
mock_logger.error.assert_not_called()
mock_send.assert_not_called()
mock_send.assert_called_once_with(self.api_key, self.api_host, self.processed_events)
event_manager.stop()