Skip to content

Commit df99d4a

Browse files
authored
fix:v1.1.2 (#5)
1 parent 7df44b5 commit df99d4a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

fbclient/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ def __init__(self, config: Config, start_wait: float = 15.):
9292
if not isinstance(self._update_processor, NullUpdateProcessor):
9393
log.info("FB Python SDK: Waiting for Client initialization in %s seconds" % str(start_wait))
9494

95+
update_processor_ready.wait(start_wait)
9596
if isinstance(self._data_storage, NullDataStorage) or (not self._data_storage.initialized and not self._config.is_offline):
9697
log.warning("FB Python SDK: SDK just returns default variation because of no data found in the given environment")
97-
98-
update_processor_ready.wait(start_wait)
9998
if not self._update_processor.initialized:
10099
log.warning("FB Python SDK: SDK was not successfully initialized")
101100
else:

fbclient/event_processor.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from concurrent.futures import ThreadPoolExecutor
33
from queue import Empty, Queue
4-
from threading import BoundedSemaphore, Lock, Thread
4+
from threading import BoundedSemaphore, Condition, Lock, Thread
55
from typing import List, Optional
66

77
from fbclient.common_types import FBEvent
@@ -76,7 +76,7 @@ def stop(self):
7676
log.info('FB Python SDK: event processor is stopping')
7777
self.__closed = True
7878
self.__flush_task.stop()
79-
self.flush()
79+
self.__put_message_async(MessageType.FLUSH)
8080
self.__put_message_and_wait_terminate(MessageType.SHUTDOWN)
8181

8282

@@ -94,6 +94,7 @@ def __init__(self, config: Config, sender: Sender, inbox: "Queue[EventMessage]")
9494
self.__events_buffer_to_next_flush = []
9595
self.__flush_workers = ThreadPoolExecutor(max_workers=self.__MAX_FLUSH_WORKERS_NUMBER)
9696
self.__permits = BoundedSemaphore(value=self.__MAX_FLUSH_WORKERS_NUMBER)
97+
self.__lock = Condition(Lock())
9798

9899
# blocks until at least one message is available and then:
99100
# 1: transfer the events to event buffer
@@ -137,6 +138,11 @@ def __put_events_to_buffer(self, event: FBEvent):
137138
self.__events_buffer_to_next_flush.append(event)
138139

139140
def __trigger_flush(self):
141+
def flush_payload_done(fn):
142+
self.__permits.release()
143+
with self.__lock:
144+
self.__lock.notify_all()
145+
140146
if not self.__closed and len(self.__events_buffer_to_next_flush) > 0:
141147
log.debug('trigger flush')
142148
# get all the current events from event buffer
@@ -146,21 +152,27 @@ def __trigger_flush(self):
146152
# get an available flush worker to send events
147153
self.__flush_workers \
148154
.submit(FlushPayloadRunner(self.__config, self.__sender, payloads).run) \
149-
.add_done_callback(lambda x: self.__permits.release())
155+
.add_done_callback(flush_payload_done)
150156
# clear the buffer for the next flush
151157
self.__events_buffer_to_next_flush.clear()
152158
# if no available flush worker, keep the events in the buffer
153159

154160
def __shutdown(self):
155161
if not self.__closed:
156-
try:
157-
log.debug('event dispatcher is cleaning up thread and conn pool')
158-
self.__closed = True
159-
log.debug('flush worker pool is stopping...')
160-
self.__flush_workers.shutdown(wait=True)
161-
self.__sender.stop()
162-
except Exception as e:
163-
log.exception('FB Python SDK: unexpected error when closing event dispatcher: %s' % str(e))
162+
with self.__lock:
163+
try:
164+
log.debug('event dispatcher is cleaning up thread and conn pool')
165+
self.__wait_until_flush_playload_worker_down()
166+
self.__closed = True
167+
log.debug('flush worker pool is stopping...')
168+
self.__flush_workers.shutdown(wait=True)
169+
self.__sender.stop()
170+
except Exception as e:
171+
log.exception('FB Python SDK: unexpected error when closing event dispatcher: %s' % str(e))
172+
173+
def __wait_until_flush_playload_worker_down(self):
174+
while self.__permits._value != self.__MAX_FLUSH_WORKERS_NUMBER:
175+
self.__lock.wait()
164176

165177

166178
class FlushPayloadRunner:

fbclient/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "1.1.1"
1+
VERSION = "1.1.2"

0 commit comments

Comments
 (0)