Class: Mysqlknife::Replica

Inherits:
Object
  • Object
show all
Defined in:
lib/mysqlknife/replica.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Replica

Returns a new instance of Replica.



5
6
7
8
9
# File 'lib/mysqlknife/replica.rb', line 5

def initialize(options = {})
  @conn   = Connection.new(options)
  @sql    = Sql.new
  @behind = options[:behind]
end

Instance Method Details

#check_status_negative(behind) ⇒ Object



11
12
13
# File 'lib/mysqlknife/replica.rb', line 11

def check_status_negative(behind)
  @behind < 0 && behind < 0 && behind <= @behind && behind != 0
end

#check_status_positive(behind) ⇒ Object



15
16
17
# File 'lib/mysqlknife/replica.rb', line 15

def check_status_positive(behind)
  @behind > 0 && behind > 0 && @behind <= behind && behind != 0
end

#skipObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mysqlknife/replica.rb', line 19

def skip
  procedure_name  = @sql.show_procedure('rds_skip_repl_error')
  exist_procedure = @conn.execute(procedure_name).first
  slave_status    = @conn.execute(@sql.slave_status).first

  return nil if slave_status.nil?
  return nil unless slave_status.include? 'Seconds_Behind_Master'

  if check_status_negative(slave_status['Seconds_Behind_Master'])
    puts %W(#{Time.now.getutc} : MySQL Skip error replica -
            #{slave_status.map { |k, v| "#{k}: #{v}" }.join(', ')}
            ).join(' ')
    if exist_procedure.nil?
      @conn.execute(@sql.mysql_skip_repl_error)
      while @conn.next_result
        message = @conn.store_result
        puts %W(#{Time.now.getutc} : MySQL Skip error replica -
                Message: #{message}).join(' ')
        sleep(1)
      end
    else
      message = @conn.execute(@sql.rds_skip_repl_error).first['Message']
      puts %W(#{Time.now.getutc} : MySQL Skip error replica -
              Message: #{message}).join(' ')
    end
  # Resolv Positive Lag:
  elsif check_status_positive(slave_status['Seconds_Behind_Master'])
    puts %W(#{Time.now.getutc} : MySQL Skip error replica -
            Not implemented.).join(' ')
  end
end