class MysqlWarmup::Warmer

Constants

ALL_DATABASE
EXCLUDE_DATABASES
PREVENT_VARIABLES

Public Class Methods

new(host, username, password, port = 3306, database = 'all') click to toggle source
# File lib/mysql_warmup/warmer.rb, line 8
def initialize(host, username, password, port = 3306, database = 'all')
  @host     = host
  @username = username
  @password = password
  @database = database
  @port     = port

  @connector = if warmup_all?
                 Mysql.new(@host, @username, @password, '', @port)
               else
                 Mysql.new(@host, @username, @password, @database, @port)
               end
end

Public Instance Methods

inspect() click to toggle source

Prevent inspection object

# File lib/mysql_warmup/warmer.rb, line 27
def inspect
  "#<MysqlWarmup::Warmer:#{object_id}>"
end
instance_variable_get(*several_variants) click to toggle source
Calls superclass method
# File lib/mysql_warmup/warmer.rb, line 31
def instance_variable_get(*several_variants)
  raise 'Not allow to view this variable' if PREVENT_VARIABLES.include?(several_variants[0])
  super
end
warmup() click to toggle source
# File lib/mysql_warmup/warmer.rb, line 22
def warmup
  warmup_all? ? warmup_all_dbs : warmup_only
end

Private Instance Methods

fetch_fields_infos(table_name) click to toggle source
# File lib/mysql_warmup/warmer.rb, line 71
def fetch_fields_infos(table_name)
  fields_infos = []
  fields       = @connector.query("describe `#{table_name}`")
  field        = fields.fetch_row
  while field
    fields_infos << field
    field = fields.fetch_row
  end
  fields_infos
end
touch(query_string) click to toggle source
# File lib/mysql_warmup/warmer.rb, line 82
def touch(query_string)
  # write_log(query_string)
  @connector.query(query_string)
end
warmup_all?() click to toggle source
# File lib/mysql_warmup/warmer.rb, line 91
def warmup_all?
  @database.downcase == ALL_DATABASE
end
warmup_all_dbs() click to toggle source
# File lib/mysql_warmup/warmer.rb, line 38
def warmup_all_dbs
  @connector.list_dbs.each do |db|
    next if EXCLUDE_DATABASES.include?(db)
    MysqlWarmup::Warmer.new(@host, @username, @password, @port, db).warmup
  end
end
warmup_only() click to toggle source
# File lib/mysql_warmup/warmer.rb, line 45
def warmup_only
  write_log(">>>>>>> START WARMUP FOR DB: #{@database} <<<<<<")
  tables = @connector.query('show tables')
  table  = tables.fetch_row
  while table
    # Fetch fields infos
    fields_infos = fetch_fields_infos(table[0])

    table_instance = MysqlWarmup::Table.new(table[0], fields_infos)
    write_log("START WARMUP FOR TABLE:   `#{@database}`.`#{table[0]}`")
    table_instance.indexes.each do |i|
      touch(i.query_string)
    end
    write_log("SUCCESS WARMUP FOR TABLE: `#{@database}`.`#{table[0]}`\n\n")

    # Continue fetching table
    table = tables.fetch_row
  end
  write_log("+++++++ SUCCESS WARMUP FOR DB: #{@database} +++++++\n\n")
rescue Mysql::Error => e
  write_log("ERROR: ----------- #{e.message}")
  write_log("BACKTRACE: ------- #{e.backtrace[0, 5]}")
ensure
  @connector.close if @connector
end
write_log(log_msg) click to toggle source
# File lib/mysql_warmup/warmer.rb, line 87
def write_log(log_msg)
  MysqlWarmup::Logger.write(log_msg)
end