|
1 | 1 | """Message processor & Notification manager keeper implementations.""" |
2 | 2 |
|
3 | 3 | from queue import Queue |
| 4 | +import abc |
4 | 5 |
|
5 | 6 | from splitio.push.parser import UpdateType |
6 | | -from splitio.push.workers import SplitWorker |
7 | | -from splitio.push.workers import SegmentWorker |
| 7 | +from splitio.push.workers import SplitWorker, SplitWorkerAsync, SegmentWorker, SegmentWorkerAsync |
| 8 | +from splitio.optional.loaders import asyncio |
8 | 9 |
|
| 10 | +class MessageProcessorBase(object, metaclass=abc.ABCMeta): |
| 11 | + """Message processor template.""" |
9 | 12 |
|
10 | | -class MessageProcessor(object): |
| 13 | + @abc.abstractmethod |
| 14 | + def update_workers_status(self, enabled): |
| 15 | + """Enable/Disable push update workers.""" |
| 16 | + |
| 17 | + @abc.abstractmethod |
| 18 | + def handle(self, event): |
| 19 | + """Handle incoming update event.""" |
| 20 | + |
| 21 | + @abc.abstractmethod |
| 22 | + def shutdown(self): |
| 23 | + """Stop splits & segments workers.""" |
| 24 | + |
| 25 | +class MessageProcessor(MessageProcessorBase): |
11 | 26 | """Message processor class.""" |
12 | 27 |
|
13 | 28 | def __init__(self, synchronizer): |
@@ -89,3 +104,87 @@ def shutdown(self): |
89 | 104 | """Stop splits & segments workers.""" |
90 | 105 | self._split_worker.stop() |
91 | 106 | self._segments_worker.stop() |
| 107 | + |
| 108 | + |
| 109 | +class MessageProcessorAsync(MessageProcessorBase): |
| 110 | + """Message processor class.""" |
| 111 | + |
| 112 | + def __init__(self, synchronizer): |
| 113 | + """ |
| 114 | + Class constructor. |
| 115 | +
|
| 116 | + :param synchronizer: synchronizer component |
| 117 | + :type synchronizer: splitio.sync.synchronizer.Synchronizer |
| 118 | + """ |
| 119 | + self._split_queue = asyncio.Queue() |
| 120 | + self._segments_queue = asyncio.Queue() |
| 121 | + self._synchronizer = synchronizer |
| 122 | + self._split_worker = SplitWorkerAsync(synchronizer.synchronize_splits, self._split_queue) |
| 123 | + self._segments_worker = SegmentWorkerAsync(synchronizer.synchronize_segment, self._segments_queue) |
| 124 | + self._handlers = { |
| 125 | + UpdateType.SPLIT_UPDATE: self._handle_split_update, |
| 126 | + UpdateType.SPLIT_KILL: self._handle_split_kill, |
| 127 | + UpdateType.SEGMENT_UPDATE: self._handle_segment_change |
| 128 | + } |
| 129 | + |
| 130 | + async def _handle_split_update(self, event): |
| 131 | + """ |
| 132 | + Handle incoming split update notification. |
| 133 | +
|
| 134 | + :param event: Incoming split change event |
| 135 | + :type event: splitio.push.parser.SplitChangeUpdate |
| 136 | + """ |
| 137 | + await self._split_queue.put(event) |
| 138 | + |
| 139 | + async def _handle_split_kill(self, event): |
| 140 | + """ |
| 141 | + Handle incoming split kill notification. |
| 142 | +
|
| 143 | + :param event: Incoming split kill event |
| 144 | + :type event: splitio.push.parser.SplitKillUpdate |
| 145 | + """ |
| 146 | + await self._synchronizer.kill_split(event.split_name, event.default_treatment, |
| 147 | + event.change_number) |
| 148 | + await self._split_queue.put(event) |
| 149 | + |
| 150 | + async def _handle_segment_change(self, event): |
| 151 | + """ |
| 152 | + Handle incoming segment update notification. |
| 153 | +
|
| 154 | + :param event: Incoming segment change event |
| 155 | + :type event: splitio.push.parser.Update |
| 156 | + """ |
| 157 | + await self._segments_queue.put(event) |
| 158 | + |
| 159 | + async def update_workers_status(self, enabled): |
| 160 | + """ |
| 161 | + Enable/Disable push update workers. |
| 162 | +
|
| 163 | + :param enabled: if True, enable workers. If False, disable them. |
| 164 | + :type enabled: bool |
| 165 | + """ |
| 166 | + if enabled: |
| 167 | + self._split_worker.start() |
| 168 | + self._segments_worker.start() |
| 169 | + else: |
| 170 | + await self._split_worker.stop() |
| 171 | + await self._segments_worker.stop() |
| 172 | + |
| 173 | + async def handle(self, event): |
| 174 | + """ |
| 175 | + Handle incoming update event. |
| 176 | +
|
| 177 | + :param event: incoming data update event. |
| 178 | + :type event: splitio.push.BaseUpdate |
| 179 | + """ |
| 180 | + try: |
| 181 | + handle = self._handlers[event.update_type] |
| 182 | + except KeyError as exc: |
| 183 | + raise Exception('no handler for notification type: %s' % event.update_type) from exc |
| 184 | + |
| 185 | + await handle(event) |
| 186 | + |
| 187 | + async def shutdown(self): |
| 188 | + """Stop splits & segments workers.""" |
| 189 | + await self._split_worker.stop() |
| 190 | + await self._segments_worker.stop() |
0 commit comments