Class: Fluent::ZmqSubInput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZmqSubInput

Returns a new instance of ZmqSubInput.



15
16
17
18
# File 'lib/fluent/plugin/in_fedmsg.rb', line 15

def initialize
  super
  require 'ffi-rzmq'
end

Instance Attribute Details

#subkeysObject (readonly)

Returns the value of attribute subkeys.



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

def subkeys
  @subkeys
end

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
23
24
# File 'lib/fluent/plugin/in_fedmsg.rb', line 20

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

#runObject



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
84
85
# File 'lib/fluent/plugin/in_fedmsg.rb', line 39

def run
  begin
    @subscriber = @context.socket(ZMQ::SUB)
    @subscriber.connect(@publisher)
    @subscriber.setsockopt(ZMQ::SNDHWM, 100)
    @subscriber.setsockopt(ZMQ::RCVHWM, 100)
    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
          record = JSON.parse(msg)
        rescue JSON::ParserError => e
          log.trace "Ignoring non-JSON message '#{msg}'"
          next
        rescue => e
          log.warn "Unknown error parsing JSON message.",:error_class => e.class, :error => e
          log.warn_backtrace
        end
        begin
          tag = @tag_prefix + "." + record['topic']
          time = record['timestamp']
          record.delete_if { |key, value| @drop_fields_arr.include? key}
          Engine.emit(tag, time, record)
        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



32
33
34
35
36
37
# File 'lib/fluent/plugin/in_fedmsg.rb', line 32

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

#startObject



26
27
28
29
30
# File 'lib/fluent/plugin/in_fedmsg.rb', line 26

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