1- # Copyright 2019-2021 Optimizely
1+ # Copyright 2019-2022, Optimizely
22# Licensed under the Apache License, Version 2.0 (the "License");
33# you may not use this file except in compliance with the License.
44# You may obtain a copy of the License at
1111# See the License for the specific language governing permissions and
1212# limitations under the License.
1313
14+ from __future__ import annotations
1415from abc import ABC , abstractmethod
1516import numbers
1617import threading
1718import time
1819
20+ from typing import Optional
1921from datetime import timedelta
2022import queue
2123
2224from optimizely import logger as _logging
2325from optimizely import notification_center as _notification_center
24- from optimizely .event_dispatcher import EventDispatcher as default_event_dispatcher
26+ from optimizely .event_dispatcher import EventDispatcher , CustomEventDispatcher
2527from optimizely .helpers import enums
2628from optimizely .helpers import validator
2729from .event_factory import EventFactory
@@ -32,7 +34,7 @@ class BaseEventProcessor(ABC):
3234 """ Class encapsulating event processing. Override with your own implementation. """
3335
3436 @abstractmethod
35- def process (self , user_event ) :
37+ def process (self , user_event : UserEvent ) -> None :
3638 """ Method to provide intermediary processing stage within event production.
3739 Args:
3840 user_event: UserEvent instance that needs to be processed and dispatched.
@@ -49,24 +51,28 @@ class BatchEventProcessor(BaseEventProcessor):
4951 maximum duration before the resulting LogEvent is sent to the EventDispatcher.
5052 """
5153
54+ class Signal :
55+ '''Used to create unique objects for sending signals to event queue.'''
56+ pass
57+
5258 _DEFAULT_QUEUE_CAPACITY = 1000
5359 _DEFAULT_BATCH_SIZE = 10
5460 _DEFAULT_FLUSH_INTERVAL = 30
5561 _DEFAULT_TIMEOUT_INTERVAL = 5
56- _SHUTDOWN_SIGNAL = object ()
57- _FLUSH_SIGNAL = object ()
62+ _SHUTDOWN_SIGNAL = Signal ()
63+ _FLUSH_SIGNAL = Signal ()
5864 LOCK = threading .Lock ()
5965
6066 def __init__ (
6167 self ,
62- event_dispatcher ,
63- logger = None ,
64- start_on_init = False ,
65- event_queue = None ,
66- batch_size = None ,
67- flush_interval = None ,
68- timeout_interval = None ,
69- notification_center = None ,
68+ event_dispatcher : Optional [ type [ EventDispatcher ] | CustomEventDispatcher ] = None ,
69+ logger : Optional [ _logging . Logger ] = None ,
70+ start_on_init : bool = False ,
71+ event_queue : Optional [ queue . Queue [ UserEvent | Signal ]] = None ,
72+ batch_size : Optional [ int ] = None ,
73+ flush_interval : Optional [ float ] = None ,
74+ timeout_interval : Optional [ float ] = None ,
75+ notification_center : Optional [ _notification_center . NotificationCenter ] = None ,
7076 ):
7177 """ BatchEventProcessor init method to configure event batching.
7278
@@ -84,43 +90,48 @@ def __init__(
8490 thread.
8591 notification_center: Optional instance of notification_center.NotificationCenter.
8692 """
87- self .event_dispatcher = event_dispatcher or default_event_dispatcher
93+ self .event_dispatcher = event_dispatcher or EventDispatcher
8894 self .logger = _logging .adapt_logger (logger or _logging .NoOpLogger ())
8995 self .event_queue = event_queue or queue .Queue (maxsize = self ._DEFAULT_QUEUE_CAPACITY )
90- self .batch_size = (
91- batch_size
96+ self .batch_size : int = (
97+ batch_size # type: ignore
9298 if self ._validate_instantiation_props (batch_size , 'batch_size' , self ._DEFAULT_BATCH_SIZE )
9399 else self ._DEFAULT_BATCH_SIZE
94100 )
95- self .flush_interval = (
96- timedelta (seconds = flush_interval )
101+ self .flush_interval : timedelta = (
102+ timedelta (seconds = flush_interval ) # type: ignore
97103 if self ._validate_instantiation_props (flush_interval , 'flush_interval' , self ._DEFAULT_FLUSH_INTERVAL )
98104 else timedelta (seconds = self ._DEFAULT_FLUSH_INTERVAL )
99105 )
100- self .timeout_interval = (
101- timedelta (seconds = timeout_interval )
106+ self .timeout_interval : timedelta = (
107+ timedelta (seconds = timeout_interval ) # type: ignore
102108 if self ._validate_instantiation_props (timeout_interval , 'timeout_interval' , self ._DEFAULT_TIMEOUT_INTERVAL )
103109 else timedelta (seconds = self ._DEFAULT_TIMEOUT_INTERVAL )
104110 )
105111
106112 self .notification_center = notification_center or _notification_center .NotificationCenter (self .logger )
107- self ._current_batch = list ()
113+ self ._current_batch : list [ UserEvent ] = []
108114
109115 if not validator .is_notification_center_valid (self .notification_center ):
110116 self .logger .error (enums .Errors .INVALID_INPUT .format ('notification_center' ))
111117 self .logger .debug ('Creating notification center for use.' )
112118 self .notification_center = _notification_center .NotificationCenter (self .logger )
113119
114- self .executor = None
120+ self .executor : Optional [ threading . Thread ] = None
115121 if start_on_init is True :
116122 self .start ()
117123
118124 @property
119- def is_running (self ):
125+ def is_running (self ) -> bool :
120126 """ Property to check if consumer thread is alive or not. """
121127 return self .executor .is_alive () if self .executor else False
122128
123- def _validate_instantiation_props (self , prop , prop_name , default_value ):
129+ def _validate_instantiation_props (
130+ self ,
131+ prop : Optional [numbers .Integral | int | float ],
132+ prop_name : str ,
133+ default_value : numbers .Integral | int | float
134+ ) -> bool :
124135 """ Method to determine if instantiation properties like batch_size, flush_interval
125136 and timeout_interval are valid.
126137
@@ -147,7 +158,7 @@ def _validate_instantiation_props(self, prop, prop_name, default_value):
147158
148159 return is_valid
149160
150- def _get_time (self , _time = None ):
161+ def _get_time (self , _time : Optional [ float ] = None ) -> float :
151162 """ Method to return time as float in seconds. If _time is None, uses current time.
152163
153164 Args:
@@ -161,7 +172,7 @@ def _get_time(self, _time=None):
161172
162173 return _time
163174
164- def start (self ):
175+ def start (self ) -> None :
165176 """ Starts the batch processing thread to batch events. """
166177 if hasattr (self , 'executor' ) and self .is_running :
167178 self .logger .warning ('BatchEventProcessor already started.' )
@@ -172,7 +183,7 @@ def start(self):
172183 self .executor .daemon = True
173184 self .executor .start ()
174185
175- def _run (self ):
186+ def _run (self ) -> None :
176187 """ Triggered as part of the thread which batches events or flushes event_queue and hangs on get
177188 for flush interval if queue is empty.
178189 """
@@ -215,12 +226,12 @@ def _run(self):
215226 self .logger .info ('Exiting processing loop. Attempting to flush pending events.' )
216227 self ._flush_batch ()
217228
218- def flush (self ):
229+ def flush (self ) -> None :
219230 """ Adds flush signal to event_queue. """
220231
221232 self .event_queue .put (self ._FLUSH_SIGNAL )
222233
223- def _flush_batch (self ):
234+ def _flush_batch (self ) -> None :
224235 """ Flushes current batch by dispatching event. """
225236 batch_len = len (self ._current_batch )
226237 if batch_len == 0 :
@@ -237,12 +248,16 @@ def _flush_batch(self):
237248
238249 self .notification_center .send_notifications (enums .NotificationTypes .LOG_EVENT , log_event )
239250
251+ if log_event is None :
252+ self .logger .exception ('Error dispatching event: Cannot dispatch None event.' )
253+ return
254+
240255 try :
241256 self .event_dispatcher .dispatch_event (log_event )
242257 except Exception as e :
243258 self .logger .error (f'Error dispatching event: { log_event } { e } ' )
244259
245- def process (self , user_event ) :
260+ def process (self , user_event : UserEvent ) -> None :
246261 """ Method to process the user_event by putting it in event_queue.
247262
248263 Args:
@@ -263,7 +278,7 @@ def process(self, user_event):
263278 f'Payload not accepted by the queue. Current size: { self .event_queue .qsize ()} '
264279 )
265280
266- def _add_to_batch (self , user_event ) :
281+ def _add_to_batch (self , user_event : UserEvent ) -> None :
267282 """ Method to append received user event to current batch.
268283
269284 Args:
@@ -283,7 +298,7 @@ def _add_to_batch(self, user_event):
283298 self .logger .debug ('Flushing on batch size.' )
284299 self ._flush_batch ()
285300
286- def _should_split (self , user_event ) :
301+ def _should_split (self , user_event : UserEvent ) -> bool :
287302 """ Method to check if current event batch should split into two.
288303
289304 Args:
@@ -308,7 +323,7 @@ def _should_split(self, user_event):
308323
309324 return False
310325
311- def stop (self ):
326+ def stop (self ) -> None :
312327 """ Stops and disposes batch event processor. """
313328 self .event_queue .put (self ._SHUTDOWN_SIGNAL )
314329 self .logger .warning ('Stopping Scheduler.' )
@@ -327,23 +342,28 @@ class ForwardingEventProcessor(BaseEventProcessor):
327342 The ForwardingEventProcessor sends the LogEvent to EventDispatcher as soon as it is received.
328343 """
329344
330- def __init__ (self , event_dispatcher , logger = None , notification_center = None ):
345+ def __init__ (
346+ self ,
347+ event_dispatcher : type [EventDispatcher ] | CustomEventDispatcher ,
348+ logger : Optional [_logging .Logger ] = None ,
349+ notification_center : Optional [_notification_center .NotificationCenter ] = None
350+ ):
331351 """ ForwardingEventProcessor init method to configure event dispatching.
332352
333353 Args:
334354 event_dispatcher: Provides a dispatch_event method which if given a URL and params sends a request to it.
335355 logger: Optional component which provides a log method to log messages. By default nothing would be logged.
336356 notification_center: Optional instance of notification_center.NotificationCenter.
337357 """
338- self .event_dispatcher = event_dispatcher or default_event_dispatcher
358+ self .event_dispatcher = event_dispatcher or EventDispatcher
339359 self .logger = _logging .adapt_logger (logger or _logging .NoOpLogger ())
340360 self .notification_center = notification_center or _notification_center .NotificationCenter (self .logger )
341361
342362 if not validator .is_notification_center_valid (self .notification_center ):
343363 self .logger .error (enums .Errors .INVALID_INPUT .format ('notification_center' ))
344364 self .notification_center = _notification_center .NotificationCenter ()
345365
346- def process (self , user_event ) :
366+ def process (self , user_event : UserEvent ) -> None :
347367 """ Method to process the user_event by dispatching it.
348368
349369 Args:
@@ -361,6 +381,10 @@ def process(self, user_event):
361381
362382 self .notification_center .send_notifications (enums .NotificationTypes .LOG_EVENT , log_event )
363383
384+ if log_event is None :
385+ self .logger .exception ('Error dispatching event: Cannot dispatch None event.' )
386+ return
387+
364388 try :
365389 self .event_dispatcher .dispatch_event (log_event )
366390 except Exception as e :
0 commit comments