class MashapeAnalytics::Capture

Public Class Methods

context() click to toggle source
# File lib/mashape-analytics/capture.rb, line 67
def self.context
  @@zmq_ctx
end
disconnect() click to toggle source

Force disconnect

# File lib/mashape-analytics/capture.rb, line 60
def self.disconnect
  return unless @connected

  @connected = false
  @thread.join
end
record!(alf) click to toggle source

Send immediately

# File lib/mashape-analytics/capture.rb, line 49
def self.record!(alf)
  if not @connected
    Capture.start
  end

  @@queue << alf
end
setOptions(options) click to toggle source
# File lib/mashape-analytics/capture.rb, line 71
def self.setOptions(options)
  @options.merge! options
end
start() click to toggle source
# File lib/mashape-analytics/capture.rb, line 17
def self.start
  return unless @thread == nil

  @thread = Thread.new do
    # Connect
    @zmq_push = @@zmq_ctx.socket(:PUSH)
    @zmq_push.connect(@options[:host])
    @connected = true

    # Send messages
    while @connected
      begin
        alf = @@queue.pop_with_timeout(1)  # 1s timeout
        @zmq_push.send 'alf_1.0.0 ' << alf.to_s
      rescue => ex
        # TODO log debug
      end
    end

    # Disconnect
    @zmq_push.close

    # Clean up
    @zmq_push = nil
    @connected = false
    @thread = nil
  end
end