lib/fluent/plugin/in_mysql_query.rb in fluent-plugin-mysql-query-0.2.1 vs lib/fluent/plugin/in_mysql_query.rb in fluent-plugin-mysql-query-0.2.2
- old
+ new
@@ -22,11 +22,11 @@
config_param :row_count_key, :string, :default => 'row_count'
config_param :record_hostname, :string, :default => nil
def configure(conf)
super
- @hostname = get_mysql_hostname
+ @hostname = nil
@interval = Config.time_value(@interval)
@nest_result = Config.bool_value(@nest_result) || false
@row_count = Config.bool_value(@row_count) || false
@record_hostname = Config.bool_value(@record_hostname) || false
$log.info "adding mysql_query job: [#{@query}] interval: #{@interval}sec"
@@ -40,10 +40,11 @@
Thread.kill(@thread)
end
def run
loop do
+ @hostname = get_mysql_hostname if @hostname.nil?
tag = "#{@tag}".gsub('__HOSTNAME__', @hostname).gsub('${hostname}', @hostname)
record = Hash.new
record.store('hostname', @hostname) if @record_hostname
result = get_exec_result
record.store(@row_count_key, result.size) if @row_count
@@ -58,26 +59,34 @@
sleep @interval
end
end
def get_connection
- return Mysql2::Client.new({
- :host => @host,
- :port => @port,
- :username => @username,
- :password => @password,
- :database => @database,
- :encoding => @encoding,
- :reconnect => true
- })
+ begin
+ return Mysql2::Client.new({
+ :host => @host,
+ :port => @port,
+ :username => @username,
+ :password => @password,
+ :database => @database,
+ :encoding => @encoding,
+ :reconnect => true
+ })
+ rescue Exception => e
+ $log.warn "mysql_query: #{e}"
+ sleep @interval
+ retry
+ end
end
def query(query)
@mysql ||= get_connection
begin
return @mysql.query(query, :cast => false, :cache_rows => false)
rescue Exception => e
- $log.info "#{e.inspect}"
+ $log.warn "mysql_query: #{e}"
+ sleep @interval
+ retry
end
end
def get_mysql_hostname
query("SHOW VARIABLES LIKE 'hostname'").each do |row|