Class: MashapeAnalytics::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/mashape-analytics/capture.rb

Constant Summary collapse

@@zmq_ctx =
ZMQ::Context.new
@@queue =
Utils::QueueWithTimeout.new

Class Method Summary collapse

Class Method Details

.contextObject



67
68
69
# File 'lib/mashape-analytics/capture.rb', line 67

def self.context
  @@zmq_ctx
end

.disconnectObject

Force disconnect



60
61
62
63
64
65
# File 'lib/mashape-analytics/capture.rb', line 60

def self.disconnect
  return unless @connected

  @connected = false
  @thread.join
end

.record!(alf) ⇒ Object

Send immediately



49
50
51
52
53
54
55
# File 'lib/mashape-analytics/capture.rb', line 49

def self.record!(alf)
  if not @connected
    Capture.start
  end

  @@queue << alf
end

.setOptions(options) ⇒ Object



71
72
73
# File 'lib/mashape-analytics/capture.rb', line 71

def self.setOptions(options)
  @options.merge! options
end

.startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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