Skip to content
This repository
Browse code

Adding some join stuff

  • Loading branch information...
commit 7f54cc55b060718f81659aef3481d0fc21db60a4 1 parent 2355120
Calvin French-Owen authored March 19, 2013
13  lib/analytics-ruby/client.rb
@@ -24,7 +24,18 @@ def initialize (options = {})
24 24
       check_secret
25 25
 
26 26
       @consumer = AnalyticsRuby::Consumer.new(@queue, @secret, options)
27  
-      Thread.new { @consumer.run }
  27
+      @thread = Thread.new { @consumer.run }
  28
+    end
  29
+
  30
+    # public: Join on the thread to close
  31
+    #
  32
+    def close ()
  33
+      @consumer.close
  34
+      if @queue.length > 0
  35
+        @thread.join
  36
+      else
  37
+        @thread.join(1)
  38
+      end
28 39
     end
29 40
 
30 41
     # public: Tracks an event
9  lib/analytics-ruby/consumer.rb
@@ -18,6 +18,7 @@ class Consumer
18 18
     #           on_error   - Proc of what to do on an error
19 19
     #
20 20
     def initialize(queue, secret, options = {})
  21
+      @open = true
21 22
       @queue = queue
22 23
       @secret = secret
23 24
       @batch_size = options[:batch_size] || AnalyticsRuby::Defaults::Queue::BATCH_SIZE
@@ -29,7 +30,7 @@ def initialize(queue, secret, options = {})
29 30
     # public: Continuously runs the loop to check for new events
30 31
     #
31 32
     def run
32  
-      while true
  33
+      while @open || !queue.empty?
33 34
         flush
34 35
       end
35 36
     end
@@ -51,5 +52,11 @@ def flush
51 52
       @current_batch = []
52 53
     end
53 54
 
  55
+    # public: Close the consumer.
  56
+    #
  57
+    def close
  58
+      @open = false
  59
+    end
  60
+
54 61
   end
55 62
 end

0 notes on commit 7f54cc5

Please sign in to comment.
Something went wrong with that request. Please try again.