Sha256: cddc1c2eb21b93f9e00f05d27375d2e1b65088df0e22f87f893351c36384fb4d

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

module MysqlReplicationHelper
  class Agent
    class Slave < Agent
      include MysqlReplicationHelper::ErrorHandler

      def poll!
        if (configured?)
          if (error_message = slave_error)
            if (statements = sql_to_recover_from(error_message))
              statements.each do |sql|
                connection.real_query(sql)
              end
            else
              # Unrecoverable error?
            end
          end
        else
          assign_master(@options[:master])
          start!
        end
      end
      
      def configured?
        !!query("SHOW SLAVE STATUS").fetch_row
      end
      
      def slave_error
        row = query("SHOW SLAVE STATUS").fetch_row
        
        row and row[19]
      end
      
      def assign_master(master)
        master_status = master.master_status
        
        master_options =
          {
            'MASTER_HOST' => 'localhost',
            'MASTER_USER' => master.user_name,
            'MASTER_PORT' => 3306,
            'MASTER_LOG_FILE' => master_status[:master_log_file],
            'MASTER_LOG_POS' => master_status[:master_log_position]
          }.collect do |k, v|
            case (v)
            when String:
              "#{k}='#{Mysql.quote(v)}'"
            else
              "#{k}=#{v}"
            end
          end
        
        execute("CHANGE MASTER TO #{master_options * ', '}")
      end
      
      def start!
        execute("START SLAVE")
      end
      
      def user_name
        @options[:slave_user] or super
      end

      def socket_name
        @options[:slave_socket] or super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
theworkinggroup-mysql-replication-helper-0.2.0 lib/mysql_replication_helper/agent/slave.rb
mysql-replication-helper-0.2.1 lib/mysql_replication_helper/agent/slave.rb