Class: Fluent::MysqlAppenderMultiInput
- Inherits:
-
Input
- Object
- Input
- Fluent::MysqlAppenderMultiInput
- Defined in:
- lib/fluent/plugin/in_mysql_appender_multi.rb
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #format_tag(config) ⇒ Object
- #get_connection ⇒ Object
- #get_query(config, last_id) ⇒ Object
-
#initialize ⇒ MysqlAppenderMultiInput
constructor
A new instance of MysqlAppenderMultiInput.
- #poll(config) ⇒ Object
- #query(query, con = nil) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ MysqlAppenderMultiInput
Returns a new instance of MysqlAppenderMultiInput.
10 11 12 13 14 15 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 10 def initialize require 'mysql2' require 'time' require 'yaml' super end |
Instance Method Details
#configure(conf) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 27 def configure(conf) super @interval = Config.time_value(@interval) if @tag.nil? raise Fluent::ConfigError, "mysql_appender_multi: missing 'tag' parameter. Please add following line into config like 'tag appender.${name}.${event}.${primary_key}'" end end |
#format_tag(config) ⇒ Object
119 120 121 122 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 119 def format_tag(config) add_db = config['td_database'] ? config['td_database'] + '.' : '' "#{tag}.#{add_db}#{config['table_name']}" end |
#get_connection ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 124 def get_connection begin return Mysql2::Client.new({ :host => @host, :port => @port, :username => @username, :password => @password, :database => @database, :encoding => @encoding, :reconnect => true, :stream => true, :cache_rows => false }) rescue Exception => e $log.warn "mysql_appender_multi: #{e}" sleep @interval retry end end |
#get_query(config, last_id) ⇒ Object
103 104 105 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 103 def get_query(config, last_id) "SELECT #{config['columns'].join(",")} FROM #{config['table_name']} where #{config['primary_key']} > #{last_id} order by #{config['primary_key']} asc limit #{config['limit']}" end |
#poll(config) ⇒ Object
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 58 def poll(config) begin @mutex.synchronize { $log.info "mysql_replicator_multi: polling start. :tag=>#{format_tag(config)}" } con = get_connection() last_id = config['last_id'] loop do rows_count = 0 start_time = Time.now rows, con = query(get_query(config, last_id), con) rows.each_with_index do |row, index| tag = format_tag(config) if @time_column.nil? then td_time = Engine.now else if row[@time_column].kind_of?(Time) then td_time = row[config['time_column']].to_i else td_time = Time.parse(row[config['time_column']].to_s).to_i end end row.each {|k, v| row[k] = v.to_s if v.is_a?(Time) || v.is_a?(Date) || v.is_a?(BigDecimal)} router.emit(tag, td_time, row) rows_count += 1 if index == rows.size - 1 @last_id = row[config['primary_key']] end end con.close elapsed_time = sprintf("%0.02f", Time.now - start_time) @mutex.synchronize { $log.info "mysql_appender_multi: finished execution :tag=>#{tag} :rows_count=>#{rows_count} :last_id=>#{last_id} :elapsed_time=>#{elapsed_time} sec" } sleep @interval end rescue StandardError => e @mutex.synchronize { $log.error "mysql_appender_multi: failed to execute query. :config=>#{masked_config}" $log.error "error: #{e.}" $log.error e.backtrace.join("\n") } end end |
#query(query, con = nil) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 107 def query(query, con = nil) begin con = con.nil? ? get_connection : con con = con.ping ? con : get_connection return con.query(query), con rescue Exception => e $log.warn "mysql_appender_multi: #{e}" sleep @interval retry end end |
#shutdown ⇒ Object
52 53 54 55 56 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 52 def shutdown @threads.each do |thread| Thread.kill(thread) end end |
#start ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fluent/plugin/in_mysql_appender_multi.rb', line 36 def start begin @threads = [] @mutex = Mutex.new YAML.load_file(@yaml_path).each do |config| @threads << Thread.new { poll(config) } end $log.error "mysql_appender_multi: stop working due to empty configuration" if @threads.empty? rescue StandardError => e $log.error "error: #{e.}" $log.error e.backtrace.join("\n") end end |