Class: Cluster::Refresh

Inherits:
Object
  • Object
show all
Includes:
Mongrel::Command::Base
Defined in:
lib/mongrel-cluster-refresh/init.rb

Instance Method Summary collapse

Instance Method Details

#collect_mongrel_statsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mongrel-cluster-refresh/init.rb', line 43

def collect_mongrel_stats
  start_port = @options["port"].to_i
  end_port = start_port + @options["servers"].to_i - 1
  
  mongrel_instance_base = {
    :port => nil,
    :pid => nil,
    :cpu => nil,
    :mem => nil
  }
  
  @mongrels = (start_port..end_port).inject([]) do |mongrels, port|
    mongrel = mongrel_instance_base.dup
    begin
      mongrel[:port] = port
      mongrel[:pid] = File.read(port_pid_file(port)).strip.to_i
      
      process_string = `ps -p #{mongrel[:pid]} -o pcpu= -o pmem=`

      if(process_string.nil? || process_string.strip == "")
        if @clean
          File.unlink(port_pid_file(port))
          puts "missing process: removing #{port_pid_file(port)}"
        end
        
        next(mongrels)
      end

      cpu, mem = process_string.split(" ").map { |field| field.strip }
      
      mongrel[:cpu] = cpu.to_f
      mongrel[:mem] = mem.to_f

      mongrels << mongrel
    ensure
      mongrels
    end
  end

  @mongrels = @mongrels.sort_by { |mongrel| [-mongrel[:mem], -mongrel[:cpu], mongrel[:pid]] }
end

#configureObject



10
11
12
13
14
15
16
# File 'lib/mongrel-cluster-refresh/init.rb', line 10

def configure
  options [
           ['-C', '--config PATH', 'Path to cluster configuration file', :@config_file, "config/mongrel_cluster.yml"],
           ['-R', '--refresh NUM', 'Number of mongrels to refresh', :@refresh_num, nil],
           ['', '--clean', "Remove orphaned pid files", :@clean, false],
          ]
end

#port_pid_file(port) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mongrel-cluster-refresh/init.rb', line 92

def port_pid_file(port)
  pid_file_ext = File.extname(@options["pid_file"])
  pid_file_base = File.basename(@options["pid_file"], pid_file_ext)

  path = []
  path << @options["cwd"] if @options["cwd"]
  path << File.dirname(@options["pid_file"])
  pid_file_dir = File.join(path)
  
  pid_file = [pid_file_base, port].join(".") + pid_file_ext
  File.join(pid_file_dir, pid_file)
end

#read_configObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongrel-cluster-refresh/init.rb', line 29

def read_config
  @options = {
    "port" => 3000,
    "pid_file" => "tmp/pids/mongrel.pid",
    "servers" => 2
  }

  conf = YAML.load_file(@config_file)
  @options.merge!(conf) if conf

  third_of_servers = @options["servers"].to_i/3
  @refresh_num ||= (third_of_servers.zero? ? 1 : third_of_servers)
end

#refreshObject



85
86
87
88
89
90
# File 'lib/mongrel-cluster-refresh/init.rb', line 85

def refresh
  @mongrels.first(@refresh_num).each do |mongrel|
    puts "restarting mongrel on port #{mongrel[:port]}"
    Process.kill("USR2", mongrel[:pid])
  end
end

#runObject



23
24
25
26
27
# File 'lib/mongrel-cluster-refresh/init.rb', line 23

def run
  read_config
  collect_mongrel_stats
  refresh
end

#validateObject



18
19
20
21
# File 'lib/mongrel-cluster-refresh/init.rb', line 18

def validate
  valid_exists?(@config_file, "Configuration file does not exist")
  @valid
end