Class: Fluent::SQLInput

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

Defined Under Namespace

Classes: StateStore, TableElement

Constant Summary collapse

SKIP_TABLE_REGEXP =
/\Aschema_migrations\Z/i

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/in_sql.rb', line 129

def configure(conf)
  super

  @tables = conf.elements.select {|e|
    e.name == 'table'
  }.map {|e|
    te = TableElement.new
    te.configure(e)
    te
  }

  if config['all_tables']
    @all_tables = true
  end
end

#shutdownObject



209
210
211
# File 'lib/fluent/plugin/in_sql.rb', line 209

def shutdown
  @stop_flag = true
end

#startObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/fluent/plugin/in_sql.rb', line 147

def start
  @state_store = StateStore.new(@state_file)

  config = {
    :adapter => @adapter,
    :host => @host,
    :port => @port,
    :database => @database,
    :username => @username,
    :password => @password,
  }

  # creates subclass of ActiveRecord::Base so that it can have different
  # database configuration from ActiveRecord::Base.
  @base_model = Class.new(ActiveRecord::Base) do
    # base model doesn't have corresponding phisical table
    self.abstract_class = true
  end

  # ActiveRecord requires the base_model to have a name. Here sets name
  # of an anonymous class by assigning it to a constant. In Ruby, class has
  # a name of a constant assigned first
  SQLInput.const_set("BaseModel_#{rand(1<<31)}", @base_model)

  # Now base_model can have independent configuration from ActiveRecord::Base
  @base_model.establish_connection(config)

  if @all_tables
    # get list of tables from the database
    @tables = @base_model.connection.tables.map do |table_name|
      if table_name.match(SKIP_TABLE_REGEXP)
        # some tables such as "schema_migrations" should be ignored
        nil
      else
        te = TableElement.new
        te.configure({
          'table' => table_name,
          'tag' => table_name,
          'update_column' => nil,
        })
        te
      end
    end.compact
  end

  # ignore tables if TableElement#init failed
  @tables.reject! do |te|
    begin
      te.init(@tag_prefix, @base_model)
      $log.info "Selecting '#{te.table}' table"
      false
    rescue
      $log.warn "Can't handle '#{te.table}' table. Ignoring.", :error => $!
      $log.warn_backtrace $!.backtrace
      true
    end
  end

  @stop_flag = false
  @thread = Thread.new(&method(:thread_main))
end

#thread_mainObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/fluent/plugin/in_sql.rb', line 213

def thread_main
  until @stop_flag
    sleep @select_interval

    @tables.each do |t|
      begin
        last_record = @state_store.last_records[t.table]
        @state_store.last_records[t.table] = t.emit_next_records(last_record, @select_limit)
        @state_store.update!
      rescue
        $log.error "unexpected error", :error=>$!.to_s
        $log.error_backtrace
      end
    end
  end
end