Class: Fluent::NorikraFilterOutput

Inherits:
BufferedOutput
  • Object
show all
Includes:
Fluent::NorikraPlugin::InputMixin, Fluent::NorikraPlugin::OutputMixin
Defined in:
lib/fluent/plugin/out_norikra_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fluent::NorikraPlugin::OutputMixin

#fetch_event_registration, #format_stream, #prepare_target, #prepared?, #register_worker, #setup_output, #shutdown_output, #start_output, #stop_output, #write

Methods included from Fluent::NorikraPlugin::InputMixin

#fetch_worker, #insert_fetch_queue, #setup_input, #shutdown_input, #start_input, #stop_input

Instance Attribute Details

#execute_serverObject (readonly)

<server>



23
24
25
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 23

def execute_server
  @execute_server
end

#execute_server_pathObject (readonly)

<server>



23
24
25
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 23

def execute_server_path
  @execute_server_path
end

Instance Method Details

#client(opts = {}) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 86

def client(opts={})
  Norikra::Client.new(@host, @port, {
      :connect_timeout => opts[:connect_timeout] || @connect_timeout,
      :send_timeout    => opts[:send_timeout]    || @send_timeout,
      :receive_timeout => opts[:receive_timeout] || @receive_timeout,
    })
end

#configure(conf) ⇒ Object



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
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 60

def configure(conf)
  super

  @host,@port = @norikra.split(':', 2)
  @port = @port.to_i

  if !@target_map_tag && @target_map_key.nil? && @target_string.nil?
    raise Fluent::ConfigError, 'target naming not specified (target_map_tag/target_map_key/target_string)'
  end

  @execute_server = false

  conf.elements.each do |element|
    case element.name
    when 'server'
      @execute_server = true
      @execute_jruby_path = element['jruby']
      @execute_server_path = element['path']
      @execute_server_opts = element['opts']
    end
  end

  setup_output(conf, true) # <query> enabled in <default> and <target TARGET>
  setup_input(conf)
end

#fetchable?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 171

def fetchable?
  @norikra_started
end

#server_starterObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 134

def server_starter
  log.info "starting Norikra server process #{@host}:#{@port}"
  base_options = [@execute_server_path, 'start', '-H', @host, '-P', @port.to_s]
  cmd,options = if @execute_jruby_path
                  [@execute_jruby_path, [@execute_server_path, 'start', '-H', @host, '-P', @port.to_s]]
                else
                  [@execute_server_path, ['start', '-H', @host, '-P', @port.to_s]]
                end
  if @execute_server_opts
    options += @execute_server_opts.split(/ +/)
  end
  @norikra_pid = fork do
    ENV.keys.select{|k| k =~ /^(RUBY|GEM|BUNDLE|RBENV|RVM|rvm)/}.each {|k| ENV.delete(k)}
    exec([cmd, 'norikra(fluentd)'], *options)
  end
  connecting = true
  log.info "trying to confirm norikra server status..."
  while connecting
    begin
      log.debug "start to connect norikra server #{@host}:#{@port}"
      client(:connect_timeout => 1, :send_timeout => 1, :receive_timeout => 1).targets
      # discard result: no exceptions is success
      connecting = false
      next
    rescue HTTPClient::TimeoutError
      log.debug "Norikra server test connection timeout. retrying..."
    rescue Errno::ECONNREFUSED
      log.debug "Norikra server test connection refused. retrying..."
    rescue => e
      log.error "unknown error in confirming norikra server, #{e.class}:#{e.message}"
    end
    sleep 3
  end
  log.info "confirmed that norikra server #{@host}:#{@port} started."
  @norikra_started = true
end

#shutdownObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 113

def shutdown
  stop_output
  stop_input
  Process.kill(:TERM, @norikra_pid) if @execute_server

  shutdown_output
  shutdown_input

  if @execute_server
    begin
      counter = 0
      while !Process.waitpid(@norikra_pid, Process::WNOHANG)
        sleep 1
        break if counter > 3
      end
    rescue Errno::ECHILD
      # norikra server process exited.
    end
  end
end

#startObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fluent/plugin/out_norikra_filter.rb', line 94

def start
  super

  @norikra_started = false

  if @execute_server
    @norikra_pid = nil
    @norikra_thread = Thread.new(&method(:server_starter))
    # @norikra_started will be set in server_starter
  else
    @norikra_pid = nil
    @norikra_thread = nil
    @norikra_started = true
  end

  start_output
  start_input
end