Class: Kodama::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kodama/client.rb

Defined Under Namespace

Classes: BinlogInfo, PositionFile, RetryInfo

Constant Summary collapse

LOG_LEVEL =
{
  :fatal => Logger::FATAL,
  :error => Logger::ERROR,
  :warn => Logger::WARN,
  :info => Logger::INFO,
  :debug => Logger::DEBUG,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.



30
31
32
33
34
35
36
37
38
39
# File 'lib/kodama/client.rb', line 30

def initialize(url)
  @url = url
  @binlog_info = BinlogInfo.new
  @retry_info = RetryInfo.new(:limit => 100, :wait => 3)
  @callbacks = {}
  @logger = Logger.new(STDOUT)
  @safe_to_stop = true

  self.log_level = :info
end

Class Method Details

.mysql_url(options = {}) ⇒ Object



23
24
25
26
27
# File 'lib/kodama/client.rb', line 23

def mysql_url(options = {})
  password = options[:password] ? ":#{options[:password]}" : nil
  port = options[:port] ? ":#{options[:port]}" : nil
  "mysql://#{options[:username]}#{password}@#{options[:host]}#{port}"
end

.start(options = {}, &block) ⇒ Object



17
18
19
20
21
# File 'lib/kodama/client.rb', line 17

def start(options = {}, &block)
  client = self.new(mysql_url(options))
  block.call(client)
  client.start
end

Instance Method Details

#binlog_client(url) ⇒ Object



81
82
83
# File 'lib/kodama/client.rb', line 81

def binlog_client(url)
  Binlog::Client.new(url)
end

#binlog_position_file=(filename) ⇒ Object



52
53
54
55
# File 'lib/kodama/client.rb', line 52

def binlog_position_file=(filename)
  @position_file = position_file(filename)
  @binlog_info.load!(@position_file)
end

#connection_retry_countObject



89
90
91
# File 'lib/kodama/client.rb', line 89

def connection_retry_count
  @retry_info.count
end

#connection_retry_limit=(limit) ⇒ Object



61
62
63
# File 'lib/kodama/client.rb', line 61

def connection_retry_limit=(limit)
  @retry_info.limit = limit
end

#connection_retry_wait=(wait) ⇒ Object



57
58
59
# File 'lib/kodama/client.rb', line 57

def connection_retry_wait=(wait)
  @retry_info.wait = wait
end

#gracefully_stop_on(*signals) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kodama/client.rb', line 69

def gracefully_stop_on(*signals)
  signals.each do |signal|
    Signal.trap(signal) do
      if safe_to_stop?
        exit(0)
      else
        stop_request
      end
    end
  end
end

#log_level=(level) ⇒ Object



65
66
67
# File 'lib/kodama/client.rb', line 65

def log_level=(level)
  @logger.level = LOG_LEVEL[level]
end

#on_format_event(&block) ⇒ Object



45
# File 'lib/kodama/client.rb', line 45

def on_format_event(&block); @callbacks[:on_format_event] = block; end

#on_incident_event(&block) ⇒ Object



49
# File 'lib/kodama/client.rb', line 49

def on_incident_event(&block); @callbacks[:on_incident_event] = block; end

#on_int_var_event(&block) ⇒ Object



43
# File 'lib/kodama/client.rb', line 43

def on_int_var_event(&block); @callbacks[:on_int_var_event] = block; end

#on_query_event(&block) ⇒ Object



41
# File 'lib/kodama/client.rb', line 41

def on_query_event(&block); @callbacks[:on_query_event] = block; end

#on_rotate_event(&block) ⇒ Object



42
# File 'lib/kodama/client.rb', line 42

def on_rotate_event(&block); @callbacks[:on_rotate_event] = block; end

#on_row_event(&block) ⇒ Object



48
# File 'lib/kodama/client.rb', line 48

def on_row_event(&block); @callbacks[:on_row_event] = block; end

#on_table_map_event(&block) ⇒ Object



47
# File 'lib/kodama/client.rb', line 47

def on_table_map_event(&block); @callbacks[:on_table_map_event] = block; end

#on_unimplemented_event(&block) ⇒ Object



50
# File 'lib/kodama/client.rb', line 50

def on_unimplemented_event(&block); @callbacks[:on_unimplemented_event] = block; end

#on_user_var_event(&block) ⇒ Object



44
# File 'lib/kodama/client.rb', line 44

def on_user_var_event(&block); @callbacks[:on_user_var_event] = block; end

#on_xid(&block) ⇒ Object



46
# File 'lib/kodama/client.rb', line 46

def on_xid(&block); @callbacks[:on_xid] = block; end

#position_file(filename) ⇒ Object



85
86
87
# File 'lib/kodama/client.rb', line 85

def position_file(filename)
  PositionFile.new(filename)
end

#safe_to_stop?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/kodama/client.rb', line 93

def safe_to_stop?
  !!@safe_to_stop
end

#startObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/kodama/client.rb', line 101

def start
  begin
    client = binlog_client(@url)
    raise Binlog::Error, 'MySQL server has gone away' unless client.connect
    @retry_info.count_reset

    if @binlog_info.valid?
      client.set_position(@binlog_info.filename, @binlog_info.position)
    end

    while event = client.wait_for_next_event
      unsafe do
        process_event(event)
      end
      break if stop_requested?
    end
  rescue Binlog::Error => e
    @logger.debug e
    if client.closed? && @retry_info.retryable?
      sleep @retry_info.wait
      @retry_info.count_up
      retry
    end
    raise e
  end
end

#stop_requestObject



97
98
99
# File 'lib/kodama/client.rb', line 97

def stop_request
  @stop_requested = true
end