Skip to content

Commit c5a8a34

Browse files
committed
Synchronous flushing now waits for existing threads to finish
1 parent d3f7803 commit c5a8a34

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

snowplow_tracker/consumer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
logger.setLevel(logging.DEBUG)
3434

3535
DEFAULT_MAX_LENGTH = 10
36+
THREAD_TIMEOUT = 10
3637

3738
new_contract("protocol", lambda x: x == "http" or x == "https")
3839

@@ -71,7 +72,7 @@ def __init__(self, endpoint, protocol="http", port=None, method="get", buffer_si
7172
:param method: The HTTP request method
7273
:type method: method
7374
:param buffer_size: The maximum number of queued events before the buffer is flushed. Default is 10.
74-
:type buffer_size: string | None
75+
:type buffer_size: int | None
7576
:param on_success: Callback executed after every HTTP request in a flush has status code 200
7677
Gets passed the number of events flushed.
7778
:type on_success: function | None
@@ -97,6 +98,8 @@ def __init__(self, endpoint, protocol="http", port=None, method="get", buffer_si
9798
self.on_success = on_success
9899
self.on_failure = on_failure
99100

101+
self.threads = []
102+
100103
@staticmethod
101104
@contract
102105
def as_collector_uri(endpoint, protocol="http", port=None):
@@ -197,16 +200,25 @@ def sync_flush(self):
197200
"""
198201
logger.debug("Starting synchronous flush...")
199202
result = Consumer.flush(self)
203+
for t in self.threads:
204+
t.join(THREAD_TIMEOUT)
200205
logger.debug("Finished synchrous flush")
201206
return result
202207

208+
203209
class AsyncConsumer(Consumer):
204210
"""
205211
Uses threads to send HTTP requests asynchronously
206212
"""
207213
def flush(self):
214+
"""
215+
Removes all dead threads, then creates a new thread which
216+
excecutes the flush method of the base Consumer class
217+
"""
218+
self.threads = [t for t in self.threads if t.isAlive()]
208219
logger.debug("Flushing thread running...")
209220
t = threading.Thread(target=super(AsyncConsumer, self).flush)
221+
self.threads.append(t)
210222
t.start()
211223

212224

snowplow_tracker/tracker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def track_unstruct_event(self, event_vendor, event_name, dict_, context=None, ts
366366
return self.complete_payload(pb)
367367

368368
@contract
369-
def flush(self, async=True):
369+
def flush(self, async=False):
370370
"""
371371
Flush the consumer
372372
@@ -375,7 +375,8 @@ def flush(self, async=True):
375375
:rtype: tracker | int
376376
"""
377377
if async:
378-
return self.out_queue.flush()
378+
self.out_queue.flush()
379+
return self
379380
else:
380381
return self.out_queue.sync_flush()
381382

0 commit comments

Comments
 (0)