22
22
from optimizely .event_dispatcher import EventDispatcher as default_event_dispatcher
23
23
from optimizely .helpers import enums
24
24
from optimizely .helpers import validator
25
- from .user_event import UserEvent
26
25
from .event_factory import EventFactory
26
+ from .user_event import UserEvent
27
27
28
28
ABC = abc .ABCMeta ('ABC' , (object ,), {'__slots__' : ()})
29
29
@@ -33,12 +33,16 @@ class BaseEventProcessor(ABC):
33
33
34
34
@abc .abstractmethod
35
35
def process (user_event ):
36
+ """ Method to provide intermediary processing stage within event production.
37
+ Args:
38
+ user_event: UserEvent instance that needs to be processed and dispatched.
39
+ """
36
40
pass
37
41
38
42
39
43
class BatchEventProcessor (BaseEventProcessor ):
40
44
"""
41
- BatchEventProcessor is a batched implementation of the BaseEventProcessor.
45
+ BatchEventProcessor is an implementation of the BaseEventProcessor that batches events .
42
46
The BatchEventProcessor maintains a single consumer thread that pulls events off of
43
47
the blocking queue and buffers them for either a configured batch size or for a
44
48
maximum duration before the resulting LogEvent is sent to the EventDispatcher.
@@ -89,17 +93,15 @@ def __init__(self,
89
93
if self ._validate_intantiation_props (timeout_interval , 'timeout_interval' ) \
90
94
else self ._DEFAULT_TIMEOUT_INTERVAL
91
95
self .notification_center = notification_center
92
-
93
- self ._is_started = False
94
96
self ._current_batch = list ()
95
97
96
98
if start_on_init is True :
97
99
self .start ()
98
100
99
101
@property
100
- def is_started (self ):
102
+ def is_running (self ):
101
103
""" Property to check if consumer thread is alive or not. """
102
- return self ._is_started
104
+ return self .executor . isAlive ()
103
105
104
106
def _validate_intantiation_props (self , prop , prop_name ):
105
107
""" Method to determine if instantiation properties like batch_size, flush_interval
@@ -137,17 +139,15 @@ def _get_time(self, _time=None):
137
139
138
140
def start (self ):
139
141
""" Starts the batch processing thread to batch events. """
140
- if self . is_started :
141
- self .logger .warning ('Service already started' )
142
+ if hasattr ( self , 'executor' ) and self . is_running :
143
+ self .logger .warning ('BatchEventProcessor already started. ' )
142
144
return
143
145
144
146
self .flushing_interval_deadline = self ._get_time () + self ._get_time (self .flush_interval .total_seconds ())
145
147
self .executor = threading .Thread (target = self ._run )
146
148
self .executor .setDaemon (True )
147
149
self .executor .start ()
148
150
149
- self ._is_started = True
150
-
151
151
def _run (self ):
152
152
""" Triggered as part of the thread which batches events or flushes event_queue and sleeps
153
153
periodically if queue is empty.
@@ -212,6 +212,10 @@ def _flush_queue(self):
212
212
self .logger .error ('Error dispatching event: ' + str (log_event ) + ' ' + str (e ))
213
213
214
214
def process (self , user_event ):
215
+ """ Method to process the user_event by putting it in event_queue.
216
+ Args:
217
+ user_event: UserEvent Instance.
218
+ """
215
219
if not isinstance (user_event , UserEvent ):
216
220
self .logger .error ('Provided event is in an invalid format.' )
217
221
return
@@ -255,12 +259,10 @@ def _should_split(self, user_event):
255
259
256
260
def stop (self ):
257
261
""" Stops and disposes batch event processor. """
258
-
259
262
self .event_queue .put (self ._SHUTDOWN_SIGNAL )
263
+ self .logger .warning ('Stopping Scheduler.' )
264
+
260
265
self .executor .join (self .timeout_interval .total_seconds ())
261
266
262
- if self .executor . isAlive () :
267
+ if self .is_running :
263
268
self .logger .error ('Timeout exceeded while attempting to close for ' + str (self .timeout_interval ) + ' ms.' )
264
-
265
- self .logger .warning ('Stopping Scheduler.' )
266
- self ._is_started = False
0 commit comments