Class: Fluent::ZmqSubInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_zmq_sub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZmqSubInput

Returns a new instance of ZmqSubInput.



12
13
14
15
# File 'lib/fluent/plugin/in_zmq_sub.rb', line 12

def initialize
  super
  require 'ffi-rzmq'
end

Instance Attribute Details

#subkeysObject (readonly)

Returns the value of attribute subkeys.



10
11
12
# File 'lib/fluent/plugin/in_zmq_sub.rb', line 10

def subkeys
  @subkeys
end

Instance Method Details

#configure(conf) ⇒ Object



17
18
19
20
# File 'lib/fluent/plugin/in_zmq_sub.rb', line 17

def configure(conf)
  super
  @subkeys = @subkey.split(",")
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/plugin/in_zmq_sub.rb', line 35

def run
  begin
    @subscriber = @context.socket(ZMQ::SUB)
    @subscriber.connect(@publisher)
    if @subkeys.size > 0
      @subkeys.each do |k|
        @subscriber.setsockopt(ZMQ::SUBSCRIBE,k)
      end
    else
      @subscriber.setsockopt(ZMQ::SUBSCRIBE,'')
    end
    loop do
      msg = ''
      while @subscriber.recv_string(msg,ZMQ::DONTWAIT) && msg.size > 0
        begin
          (key, records) = msg.split(" ",2)
          records = MessagePack.unpack(records)
          if @bulk_send && records[0].class == Array
            es = MultiEventStream.new
            prev_tag = nil
            records.each do |tag, time, record|
              if prev_tag && prev_tag != tag
                Engine.emit_stream(prev_tag, es)
                es = MultiEventStream.new
              end
              es.add(time, record)
              prev_tag = tag
            end
            Engine.emit_stream(prev_tag, es) if es.to_a.size > 0
          else
            Engine.emit(*records)
          end
        rescue => e
          log.warn "Error in processing message.",:error_class => e.class, :error => e
          log.warn_backtrace
        end
        msg = ''
      end
      sleep(0.1)
    end
  rescue => e
    log.error "error occurred while executing plugin.", :error_class => e.class, :error => e
    log.warn_backtrace
  ensure
    if @subscriber
      @subscriber.close
    end
  end
end

#shutdownObject



28
29
30
31
32
33
# File 'lib/fluent/plugin/in_zmq_sub.rb', line 28

def shutdown
  super
  Thread.kill(@thread)
  @thread.join
  @context.terminate
end

#startObject



22
23
24
25
26
# File 'lib/fluent/plugin/in_zmq_sub.rb', line 22

def start
  super
  @context =ZMQ::Context.new()
  @thread = Thread.new(&method(:run))
end