1- """Feature Flag changes processing worker."""
1+ """Segment changes processing worker."""
22import logging
33import threading
44import abc
55
66from splitio .optional .loaders import asyncio
77
8+
89_LOGGER = logging .getLogger (__name__ )
910
10- class SplitWorkerBase (object , metaclass = abc .ABCMeta ):
11- """HttpClient wrapper template."""
11+ class WorkerBase (object , metaclass = abc .ABCMeta ):
12+ """Worker template."""
1213
1314 @abc .abstractmethod
1415 def is_running (self ):
@@ -22,7 +23,125 @@ def start(self):
2223 def stop (self ):
2324 """Stop worker."""
2425
25- class SplitWorker (SplitWorkerBase ):
26+ class SegmentWorker (WorkerBase ):
27+ """Segment Worker for processing updates."""
28+
29+ _centinel = object ()
30+
31+ def __init__ (self , synchronize_segment , segment_queue ):
32+ """
33+ Class constructor.
34+
35+ :param synchronize_segment: handler to perform segment synchronization on incoming event
36+ :type synchronize_segment: function
37+
38+ :param segment_queue: queue with segment updates notifications
39+ :type segment_queue: queue
40+ """
41+ self ._segment_queue = segment_queue
42+ self ._handler = synchronize_segment
43+ self ._running = False
44+ self ._worker = None
45+
46+ def is_running (self ):
47+ """Return whether the working is running."""
48+ return self ._running
49+
50+ def _run (self ):
51+ """Run worker handler."""
52+ while self .is_running ():
53+ event = self ._segment_queue .get ()
54+ if not self .is_running ():
55+ break
56+ if event == self ._centinel :
57+ continue
58+ _LOGGER .debug ('Processing segment_update: %s, change_number: %d' ,
59+ event .segment_name , event .change_number )
60+ try :
61+ self ._handler (event .segment_name , event .change_number )
62+ except Exception :
63+ _LOGGER .error ('Exception raised in segment synchronization' )
64+ _LOGGER .debug ('Exception information: ' , exc_info = True )
65+
66+ def start (self ):
67+ """Start worker."""
68+ if self .is_running ():
69+ _LOGGER .debug ('Worker is already running' )
70+ return
71+ self ._running = True
72+
73+ _LOGGER .debug ('Starting Segment Worker' )
74+ self ._worker = threading .Thread (target = self ._run , name = 'PushSegmentWorker' , daemon = True )
75+ self ._worker .start ()
76+
77+ def stop (self ):
78+ """Stop worker."""
79+ _LOGGER .debug ('Stopping Segment Worker' )
80+ if not self .is_running ():
81+ _LOGGER .debug ('Worker is not running. Ignoring.' )
82+ return
83+ self ._running = False
84+ self ._segment_queue .put (self ._centinel )
85+
86+ class SegmentWorkerAsync (WorkerBase ):
87+ """Segment Worker for processing updates."""
88+
89+ _centinel = object ()
90+
91+ def __init__ (self , synchronize_segment , segment_queue ):
92+ """
93+ Class constructor.
94+
95+ :param synchronize_segment: handler to perform segment synchronization on incoming event
96+ :type synchronize_segment: function
97+
98+ :param segment_queue: queue with segment updates notifications
99+ :type segment_queue: asyncio.Queue
100+ """
101+ self ._segment_queue = segment_queue
102+ self ._handler = synchronize_segment
103+ self ._running = False
104+
105+ def is_running (self ):
106+ """Return whether the working is running."""
107+ return self ._running
108+
109+ async def _run (self ):
110+ """Run worker handler."""
111+ while self .is_running ():
112+ event = await self ._segment_queue .get ()
113+ if not self .is_running ():
114+ break
115+ if event == self ._centinel :
116+ continue
117+ _LOGGER .debug ('Processing segment_update: %s, change_number: %d' ,
118+ event .segment_name , event .change_number )
119+ try :
120+ await self ._handler (event .segment_name , event .change_number )
121+ except Exception :
122+ _LOGGER .error ('Exception raised in segment synchronization' )
123+ _LOGGER .debug ('Exception information: ' , exc_info = True )
124+
125+ def start (self ):
126+ """Start worker."""
127+ if self .is_running ():
128+ _LOGGER .debug ('Worker is already running' )
129+ return
130+ self ._running = True
131+
132+ _LOGGER .debug ('Starting Segment Worker' )
133+ asyncio .get_event_loop ().create_task (self ._run ())
134+
135+ async def stop (self ):
136+ """Stop worker."""
137+ _LOGGER .debug ('Stopping Segment Worker' )
138+ if not self .is_running ():
139+ _LOGGER .debug ('Worker is not running. Ignoring.' )
140+ return
141+ self ._running = False
142+ await self ._segment_queue .put (self ._centinel )
143+
144+ class SplitWorker (WorkerBase ):
26145 """Feature Flag Worker for processing updates."""
27146
28147 _centinel = object ()
@@ -81,7 +200,7 @@ def stop(self):
81200 self ._running = False
82201 self ._feature_flag_queue .put (self ._centinel )
83202
84- class SplitWorkerAsync (SplitWorkerBase ):
203+ class SplitWorkerAsync (WorkerBase ):
85204 """Split Worker for processing updates."""
86205
87206 _centinel = object ()
0 commit comments