Module: Repctl::Servers
Instance Method Summary collapse
- #all_instances ⇒ Object
- #all_live_instances ⇒ Object
- #all_live_servers ⇒ Object
- #all_servers ⇒ Object
-
#get_mysqld_pid(instance) ⇒ Object
Return the process ID (pid) for an instance.
- #instance_for(host, port) ⇒ Object
- #live?(instance) ⇒ Boolean
-
#mysqld_running?(pid) ⇒ Boolean
See if a MySQL server with the given pid is running.
- #server_for_instance(instance) ⇒ Object
Instance Method Details
#all_instances ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/repctl/servers.rb', line 13 def all_instances instances = [] all_servers.each do |s| instances << s["instance"] end instances end |
#all_live_instances ⇒ Object
35 36 37 |
# File 'lib/repctl/servers.rb', line 35 def all_live_instances all_live_servers.map {|s| s["instance"]} end |
#all_live_servers ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/repctl/servers.rb', line 25 def all_live_servers s = all_servers.select do |s| if pid = get_mysqld_pid(s["instance"]) mysqld_running?(pid) else false end end end |
#all_servers ⇒ Object
9 10 11 |
# File 'lib/repctl/servers.rb', line 9 def all_servers @servers ||= File.open(SERVER_CONFIG) { |yf| YAML::load( yf ) } end |
#get_mysqld_pid(instance) ⇒ Object
Return the process ID (pid) for an instance. This only consults the PID file.
59 60 61 62 63 64 |
# File 'lib/repctl/servers.rb', line 59 def get_mysqld_pid(instance) server = server_for_instance(instance) pidfile = server['pid-file'] return nil unless File.exist?(pidfile) Integer(File.open(pidfile, &:readline).strip) end |
#instance_for(host, port) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/repctl/servers.rb', line 43 def instance_for(host, port) @servers.each do |s| if s["hostname"] == host && s["port"].to_i == port.to_i return s["instance"].to_i end end return nil end |
#live?(instance) ⇒ Boolean
39 40 41 |
# File 'lib/repctl/servers.rb', line 39 def live?(instance) get_mysqld_pid(instance) end |
#mysqld_running?(pid) ⇒ Boolean
See if a MySQL server with the given pid is running.
53 54 55 56 |
# File 'lib/repctl/servers.rb', line 53 def mysqld_running?(pid) pids = %x{ ps -e | grep mysqld}.split("\n").map { |row| row =~ /\s*(\d+)\s+/; $1.to_i} pids.include?(pid) end |
#server_for_instance(instance) ⇒ Object
21 22 23 |
# File 'lib/repctl/servers.rb', line 21 def server_for_instance(instance) all_servers.select {|s| s["instance"] == Integer(instance)}.shift end |