Class: SnowplowTracker::AsyncEmitter

Inherits:
Emitter
  • Object
show all
Defined in:
lib/snowplow-tracker/emitters.rb

Instance Method Summary collapse

Methods inherited from Emitter

#input, #is_good_status_code, #send_requests

Constructor Details

#initialize(endpoint, config = {}) ⇒ AsyncEmitter

Returns a new instance of AsyncEmitter.



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/snowplow-tracker/emitters.rb', line 231

def initialize(endpoint, config={})
  @queue = Queue.new()
  # @all_processed_condition and @results_unprocessed are used to emulate Python's Queue.task_done()
  @queue.extend(MonitorMixin)
  @all_processed_condition = @queue.new_cond
  @results_unprocessed = 0
  (config[:thread_count] || 1).times do
    t = Thread.new do
      consume
    end
  end
  super(endpoint, config)
end

Instance Method Details

#consumeObject



245
246
247
248
249
250
251
252
253
254
# File 'lib/snowplow-tracker/emitters.rb', line 245

def consume
  loop do
    work_unit = @queue.pop
    send_requests(work_unit)
    @queue.synchronize do
      @results_unprocessed -= 1
      @all_processed_condition.broadcast
    end
  end
end

#flush(async = true) ⇒ Object

Flush the buffer

If async is false, block until the queue is empty


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/snowplow-tracker/emitters.rb', line 259

def flush(async=true)
  loop do
    @lock.synchronize do
      @queue.synchronize do
        @results_unprocessed += 1
      end
      @queue << @buffer
      @buffer = []
    end
    if not async
      LOGGER.info('Starting synchronous flush')
      @queue.synchronize do
        @all_processed_condition.wait_while { @results_unprocessed > 0 }
        LOGGER.info('Finished synchronous flush')
      end
    end
    break if @buffer.size < 1
  end
end