Skip to content

Commit 65fb6fa

Browse files
committed
fix: address review comments.
1 parent 443b7aa commit 65fb6fa

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

optimizely/event/event_processor.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from optimizely import logger as _logging
2222
from optimizely.event_dispatcher import EventDispatcher as default_event_dispatcher
2323
from optimizely.helpers import validator
24-
from .user_event import UserEvent
2524
from .event_factory import EventFactory
25+
from .user_event import UserEvent
2626

2727
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
2828

@@ -32,12 +32,16 @@ class BaseEventProcessor(ABC):
3232

3333
@abc.abstractmethod
3434
def process(user_event):
35+
""" Method to provide intermediary processing stage within event production.
36+
Args:
37+
user_event: UserEvent instance that needs to be processed and dispatched.
38+
"""
3539
pass
3640

3741

3842
class BatchEventProcessor(BaseEventProcessor):
3943
"""
40-
BatchEventProcessor is a batched implementation of the BaseEventProcessor.
44+
BatchEventProcessor is an implementation of the BaseEventProcessor that batches events.
4145
The BatchEventProcessor maintains a single consumer thread that pulls events off of
4246
the blocking queue and buffers them for either a configured batch size or for a
4347
maximum duration before the resulting LogEvent is sent to the EventDispatcher.
@@ -84,16 +88,15 @@ def __init__(self,
8488
self.timeout_interval = timedelta(seconds=timeout_interval) \
8589
if self._validate_intantiation_props(timeout_interval, 'timeout_interval') \
8690
else self._DEFAULT_TIMEOUT_INTERVAL
87-
self._is_started = False
8891
self._current_batch = list()
8992

9093
if start_on_init is True:
9194
self.start()
9295

9396
@property
94-
def is_started(self):
97+
def is_running(self):
9598
""" Property to check if consumer thread is alive or not. """
96-
return self._is_started
99+
return self.executor.isAlive()
97100

98101
def _validate_intantiation_props(self, prop, prop_name):
99102
""" Method to determine if instantiation properties like batch_size, flush_interval
@@ -131,17 +134,15 @@ def _get_time(self, _time=None):
131134

132135
def start(self):
133136
""" Starts the batch processing thread to batch events. """
134-
if self.is_started:
135-
self.logger.warning('Service already started')
137+
if hasattr(self, 'executor') and self.is_running:
138+
self.logger.warning('BatchEventProcessor already started.')
136139
return
137140

138141
self.flushing_interval_deadline = self._get_time() + self._get_time(self.flush_interval.total_seconds())
139142
self.executor = threading.Thread(target=self._run)
140143
self.executor.setDaemon(True)
141144
self.executor.start()
142145

143-
self._is_started = True
144-
145146
def _run(self):
146147
""" Triggered as part of the thread which batches events or flushes event_queue and sleeps
147148
periodically if queue is empty.
@@ -200,6 +201,10 @@ def _flush_queue(self):
200201
self.logger.error('Error dispatching event: ' + str(log_event) + ' ' + str(e))
201202

202203
def process(self, user_event):
204+
""" Method to process the user_event by putting it in event_queue.
205+
Args:
206+
user_event: UserEvent Instance.
207+
"""
203208
if not isinstance(user_event, UserEvent):
204209
self.logger.error('Provided event is in an invalid format.')
205210
return
@@ -243,12 +248,10 @@ def _should_split(self, user_event):
243248

244249
def stop(self):
245250
""" Stops and disposes batch event processor. """
246-
247251
self.event_queue.put(self._SHUTDOWN_SIGNAL)
252+
self.logger.warning('Stopping Scheduler.')
253+
248254
self.executor.join(self.timeout_interval.total_seconds())
249255

250-
if self.executor.isAlive():
256+
if self.is_running:
251257
self.logger.error('Timeout exceeded while attempting to close for ' + str(self.timeout_interval) + ' ms.')
252-
253-
self.logger.warning('Stopping Scheduler.')
254-
self._is_started = False

tests/test_event_processor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ def test_stop_and_start(self):
263263
event_dispatcher.expect_conversion(self.event_name, self.test_user_id)
264264

265265
self._event_processor.start()
266-
self.assertStrictTrue(self._event_processor.is_started)
266+
self.assertStrictTrue(self._event_processor.is_running)
267267

268268
self._event_processor.stop()
269-
self.assertStrictFalse(self._event_processor.is_started)
269+
self.assertStrictFalse(self._event_processor.is_running)
270270

271271
self.assertEqual(0, self._event_processor.event_queue.qsize())
272272

0 commit comments

Comments
 (0)