Class: Test::Mysqld

Inherits:
Object
  • Object
show all
Defined in:
lib/test/mysqld.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mysqld

Returns a new instance of Mysqld.



23
24
25
26
27
28
# File 'lib/test/mysqld.rb', line 23

def initialize(options={})
  parse_options options
  raise "mysqld is alread running (#{mycnf["pid-file"]})" if FileTest.exist? mycnf["pid-file"]
  setup
  start if options[:auto_start] || self.class.auto_start
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



109
110
111
# File 'lib/test/mysqld.rb', line 109

def base_dir
  @base_dir
end

#mycnfObject (readonly)

Returns the value of attribute mycnf.



109
110
111
# File 'lib/test/mysqld.rb', line 109

def mycnf
  @mycnf
end

#mysql_install_dbObject (readonly)

Returns the value of attribute mysql_install_db.



109
110
111
# File 'lib/test/mysqld.rb', line 109

def mysql_install_db
  @mysql_install_db
end

#mysqldObject (readonly)

Returns the value of attribute mysqld.



109
110
111
# File 'lib/test/mysqld.rb', line 109

def mysqld
  @mysqld
end

#pidObject (readonly)

Returns the value of attribute pid.



109
110
111
# File 'lib/test/mysqld.rb', line 109

def pid
  @pid
end

Class Method Details

.auto_startObject



7
8
9
# File 'lib/test/mysqld.rb', line 7

def self.auto_start
  @@auto_start ||= false
end

.auto_start=(value) ⇒ Object



11
12
13
# File 'lib/test/mysqld.rb', line 11

def self.auto_start=(value)
  @@auto_start = !!value
end

.databaseObject



15
16
17
# File 'lib/test/mysqld.rb', line 15

def self.database
  @@database ||= 'test'
end

.database=(database) ⇒ Object



19
20
21
# File 'lib/test/mysqld.rb', line 19

def self.database=(database)
  @@database = database
end

Instance Method Details

#dsn(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/test/mysqld.rb', line 30

def dsn(options={})
  options.tap do |option|
    options[:host]     ||= mycnf["bind-address"] if mycnf["bind-address"]
    options[:port]     ||= mycnf["port"]
    options[:socket]   ||= mycnf["socket"]
    options[:username] ||= "root"
    options[:database] ||= self.class.database
  end
end

#mycnf_fileObject



40
41
42
# File 'lib/test/mysqld.rb', line 40

def mycnf_file
  File.open(base_dir + '/etc/my.cnf', 'rb'){ |f| f.read }
end

#setupObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test/mysqld.rb', line 44

def setup
  File.open(base_dir + '/etc/my.cnf', 'wb') do |f|
    f.puts "[mysqld]"
    mycnf.each do |key, val|
      if val.nil?
        f.puts "#{key}"
      else
        f.puts "#{key}=#{val}"
      end
    end
  end
  return if FileTest.directory? base_dir + '/var/mysql'

  mysql_base_dir = mysql_install_db.sub(%r'/[^/]+/mysql_install_db','')
  output = nil
  begin
    IO.popen %[#{mysql_install_db} --basedir='#{mysql_base_dir}' --defaults-file='#{base_dir}/etc/my.cnf' 2>&1] do |pipe|
      output = pipe.read
    end
  rescue
    raise "mysql_install_db failed" + (output ||= "")
  end
end

#startObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/test/mysqld.rb', line 68

def start
  return if pid

  mysqld_log = File.open(base_dir + '/tmp/mysqld.log', 'a')
  @pid = fork do
    $stdout.reopen mysqld_log
    $stderr.reopen mysqld_log
    exec %[#{mysqld} --defaults-file='#{base_dir}/etc/my.cnf' --user=root]
  end
  exit unless @pid
  mysqld_log.close

  output = nil
  begin
    while !FileTest.exist?(mycnf["pid-file"])
      if Process.waitpid pid, Process::WNOHANG
        output = File.open(base_dir + '/tmp/mysqld.log'){ |f| f.read }
      end
      sleep 0.1
    end
  rescue
    raise "mysqld failed" + (output ||= "")
  end
  create_database

  at_exit { stop }
end

#stop(signal = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/test/mysqld.rb', line 96

def stop(signal=nil)
  return unless pid
  if File.exist? mycnf["pid-file"]
    realpid = File.open(mycnf["pid-file"], "rb"){ |f| f.read }.strip.to_i
    Process.kill Signal.list[signal || "TERM"], realpid rescue nil
    Process.waitpid(realpid) rescue nil
  end
  Process.kill Signal.list[signal || "TERM"], pid rescue nil
  Process.waitpid(pid) rescue nil
  FileUtils.rm_f mycnf["pid-file"] if File.exist? mycnf["pid-file"]
  @pid = nil
end