mongrel-cluster-refresh 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.markdown +36 -0
  2. data/Rakefile +23 -0
  3. data/lib/init.rb +105 -0
  4. metadata +57 -0
data/README.markdown ADDED
@@ -0,0 +1,36 @@
1
+ # mongrel-cluster-refresh
2
+
3
+ Send graceful restart signal to most resource intensive processes in a mongrel cluster. cluster::refresh finds the cluster processes by inspecting the mongrel_cluster.yml then looking up the process' MEM and CPU usage and sorting them by MEM usage, CPU usage and process ID. Then the top N processes specified, or 1/3 of the cluster by default, are sent the USR2 signal which instructs the mongrel process to restart itself.
4
+
5
+ ## Usage
6
+
7
+ * gem install mongrel-cluster-refresh -s http://gemcutter.org
8
+ * mongrel cluster::refresh -C /path/to/mongrel_cluster.yml
9
+
10
+ ## Options
11
+
12
+ * -C, --config PATH - Path to cluster configuration file; defaults to config/mongrel_cluster.yml in the current working directory
13
+ * -R, --refresh NUM - Number of mongrels to refresh; defaults to 1/3 of the <tt>servers</tt> directive in your mongrel_cluster.yml
14
+ * --clean - Remove orphaned pid files
15
+
16
+ ## License
17
+
18
+ Copyright (c) 2009 Ryan Carmelo Briones &lt;<ryan.briones@brionesandco.com>&gt;
19
+
20
+ Permission is hereby granted, free of charge, to any person obtaining a copy
21
+ of this software and associated documentation files (the "Software"), to deal
22
+ in the Software without restriction, including without limitation the rights
23
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24
+ copies of the Software, and to permit persons to whom the Software is
25
+ furnished to do so, subject to the following conditions:
26
+
27
+ The above copyright notice and this permission notice shall be included in
28
+ all copies or substantial portions of the Software.
29
+
30
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ spec = Gem::Specification.new do |s|
5
+ s.name = 'mongrel-cluster-refresh'
6
+ s.version = '0.0.1'
7
+ s.summary = 'Send graceful restart signal to most resource intensive processes in a mongrel cluster'
8
+ s.files = FileList['[A-Z]*', 'lib/init.rb']
9
+ s.has_rdoc = false
10
+ s.author = 'Ryan Carmelo Briones'
11
+ s.email = 'ryan.briones@brionesandco.com'
12
+ s.homepage = 'http://brionesandco.com/ryanbriones'
13
+ end
14
+
15
+ package_task = Rake::GemPackageTask.new(spec) {}
16
+
17
+ task :build_gemspec do
18
+ File.open("#{spec.name}.gemspec", "w") do |f|
19
+ f.write spec.to_ruby
20
+ end
21
+ end
22
+
23
+ task :default => [:build_gemspec, :gem]
data/lib/init.rb ADDED
@@ -0,0 +1,105 @@
1
+ require 'rubygems'
2
+ require 'gem_plugin'
3
+ require 'mongrel'
4
+ require 'yaml'
5
+
6
+ module Cluster
7
+ class Refresh < GemPlugin::Plugin '/commands'
8
+ include Mongrel::Command::Base
9
+
10
+ def configure
11
+ options [
12
+ ['-C', '--config PATH', 'Path to cluster configuration file', :@config_file, "config/mongrel_cluster.yml"],
13
+ ['-R', '--refresh NUM', 'Number of mongrels to refresh', :@refresh_num, nil],
14
+ ['', '--clean', "Remove orphaned pid files", :@clean, false],
15
+ ]
16
+ end
17
+
18
+ def validate
19
+ valid_exists?(@config_file, "Configuration file does not exist")
20
+ @valid
21
+ end
22
+
23
+ def run
24
+ read_config
25
+ collect_mongrel_stats
26
+ refresh
27
+ end
28
+
29
+ def read_config
30
+ @options = {
31
+ "port" => 3000,
32
+ "pid_file" => "tmp/pids/mongrel.pid",
33
+ "servers" => 2
34
+ }
35
+
36
+ conf = YAML.load_file(@config_file)
37
+ @options.merge!(conf) if conf
38
+
39
+ third_of_servers = @options["servers"].to_i/3
40
+ @refresh_num ||= (third_of_servers.zero? ? 1 : third_of_servers)
41
+ end
42
+
43
+ def collect_mongrel_stats
44
+ start_port = @options["port"].to_i
45
+ end_port = start_port + @options["servers"].to_i - 1
46
+
47
+ mongrel_instance_base = {
48
+ :port => nil,
49
+ :pid => nil,
50
+ :cpu => nil,
51
+ :mem => nil
52
+ }
53
+
54
+ @mongrels = (start_port..end_port).inject([]) do |mongrels, port|
55
+ mongrel = mongrel_instance_base.dup
56
+ begin
57
+ mongrel[:port] = port
58
+ mongrel[:pid] = File.read(port_pid_file(port)).strip.to_i
59
+
60
+ process_string = `ps -p #{mongrel[:pid]} -o pcpu= -o pmem=`
61
+
62
+ if(process_string.nil? || process_string.strip == "")
63
+ if @clean
64
+ File.unlink(port_pid_file(port))
65
+ puts "missing process: removing #{port_pid_file(port)}"
66
+ end
67
+
68
+ next(mongrels)
69
+ end
70
+
71
+ cpu, mem = process_string.split(" ").map { |field| field.strip }
72
+
73
+ mongrel[:cpu] = cpu.to_f
74
+ mongrel[:mem] = mem.to_f
75
+
76
+ mongrels << mongrel
77
+ ensure
78
+ mongrels
79
+ end
80
+ end
81
+
82
+ @mongrels = @mongrels.sort_by { |mongrel| [-mongrel[:mem], -mongrel[:cpu], mongrel[:pid]] }
83
+ end
84
+
85
+ def refresh
86
+ @mongrels.first(@refresh_num).each do |mongrel|
87
+ puts "restarting mongrel on port #{mongrel[:port]}"
88
+ Process.kill("USR2", mongrel[:pid])
89
+ end
90
+ end
91
+
92
+ def port_pid_file(port)
93
+ pid_file_ext = File.extname(@options["pid_file"])
94
+ pid_file_base = File.basename(@options["pid_file"], pid_file_ext)
95
+
96
+ path = []
97
+ path << @options["cwd"] if @options["cwd"]
98
+ path << File.dirname(@options["pid_file"])
99
+ pid_file_dir = File.join(path)
100
+
101
+ pid_file = [pid_file_base, port].join(".") + pid_file_ext
102
+ File.join(pid_file_dir, pid_file)
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongrel-cluster-refresh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Carmelo Briones
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-26 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ryan.briones@brionesandco.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.markdown
26
+ - Rakefile
27
+ - lib/init.rb
28
+ has_rdoc: true
29
+ homepage: http://brionesandco.com/ryanbriones
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Send graceful restart signal to most resource intensive processes in a mongrel cluster
56
+ test_files: []
57
+