mongrel_cluster 0.2.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -15,15 +15,15 @@ setup_rdoc ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc']
15
15
  desc "Does a full compile, test run"
16
16
  task :default => [:test, :package]
17
17
 
18
- version="0.2.1"
18
+ version="1.0.2"
19
19
  name="mongrel_cluster"
20
20
 
21
21
  setup_gem(name, version) do |spec|
22
22
  spec.summary = "Mongrel plugin that provides commands and Capistrano tasks for managing multiple Mongrel processes."
23
23
  spec.description = spec.summary
24
24
  spec.author="Bradley Taylor"
25
- spec.add_dependency('gem_plugin', '>= 0.2.1')
26
- spec.add_dependency('mongrel', '>= 0.3.13.4')
25
+ spec.add_dependency('gem_plugin', '>= 0.2.2')
26
+ spec.add_dependency('mongrel', '>= 1.0.1')
27
27
  spec.files += Dir.glob("resources/**/*")
28
28
  spec.has_rdoc = false
29
29
  spec.files += Dir.glob("bin/*")
@@ -49,6 +49,6 @@ task :gem_source do
49
49
  rm_rf "pkg/#{name}-#{version}"
50
50
 
51
51
  sh %{ generate_yaml_index.rb -d pkg }
52
- sh %{ scp -r pkg/* #{ENV['SSH_USER']}@rubyforge.org:/var/www/gforge-projects/railsmachine/releases/ }
52
+ sh %{ scp -r pkg/* #{ENV['SSH_USER']}@rubyforge.org:/var/www/gforge-projects/mongrel/releases/ }
53
53
  end
54
54
 
@@ -8,11 +8,11 @@ def run(command, verbose)
8
8
  confs = Dir.glob("*.yml")
9
9
  confs += Dir.glob("*.conf")
10
10
  confs.each do |conf|
11
- cmd = "mongrel_rails cluster::#{command} -c #{conf}"
11
+ cmd = "mongrel_rails cluster::#{command} -C #{conf}"
12
12
  cmd += " -v" if verbose
13
- puts cmd if verbose
13
+ puts cmd if verbose || command == "status"
14
14
  output = `#{cmd}`
15
- puts output if verbose
15
+ puts output if verbose || command == "status"
16
16
  puts "mongrel_rails cluster::#{command} returned an error." unless $?.success?
17
17
  end
18
18
  end
@@ -23,7 +23,7 @@ end
23
23
  @options[:verbose] = false
24
24
 
25
25
  OptionParser.new do |opts|
26
- opts.banner = "Usage: #{$0} (start|stop|restart) [options]"
26
+ opts.banner = "Usage: #{$0} (start|stop|restart|status) [options]"
27
27
 
28
28
  opts.on("-c", "--conf_path PATH", "Path to mongrel_cluster configuration files") { |value| @options[:conf_path] = value }
29
29
  opts.on('-v', '--verbose', "Print all called commands and output.") { |value| @options[:verbose] = value }
@@ -57,8 +57,11 @@ when "restart":
57
57
  puts "Restarting all mongrel_clusters..."
58
58
  run "stop", @options[:verbose]
59
59
  run "start", @options[:verbose]
60
+ when "status":
61
+ puts "Checking all mongrel_clusters..."
62
+ run "status", @options[:verbose]
60
63
  else
61
64
  puts "Unknown command."
62
65
  end
63
66
 
64
- exit
67
+ exit
@@ -3,91 +3,228 @@ require 'mongrel'
3
3
  require 'yaml'
4
4
 
5
5
  module Cluster
6
-
7
6
  module ExecBase
8
- include Mongrel::Command::Base
9
-
10
- def validate
11
- valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.")
12
- return @valid
13
- end
7
+ include Mongrel::Command::Base
14
8
 
15
- def read_options
16
- @options = {
17
- "environment" => ENV['RAILS_ENV'] || "development",
18
- "port" => 3000,
19
- "pid_file" => "log/mongrel.pid",
20
- "servers" => 2
21
- }
22
- conf = YAML.load_file(@config_file)
23
- @options.merge! conf if conf
24
- end
9
+ STATUS_OK = 0
10
+ STATUS_ERROR = 2
11
+
12
+ def validate
13
+ valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.")
14
+ return @valid
15
+ end
25
16
 
26
- def start
27
- read_options
28
- port = @options["port"].to_i - 1
29
- pid = @options["pid_file"].split(".")
30
- puts "Starting #{@options["servers"]} Mongrel servers..."
31
- 1.upto(@options["servers"].to_i) do |i|
32
- argv = [ "mongrel_rails" ]
33
- argv << "start"
34
- argv << "-d"
35
- argv << "-e #{@options["environment"]}" if @options["environment"]
36
- argv << "-p #{port+i}"
37
- argv << "-a #{@options["address"]}" if @options["address"]
38
- argv << "-l #{@options["log_file"]}" if @options["log_file"]
39
- argv << "-P #{pid[0]}.#{port+i}.#{pid[1]}"
40
- argv << "-c #{@options["cwd"]}" if @options["cwd"]
41
- argv << "-t #{@options["timeout"]}" if @options["timeout"]
42
- argv << "-m #{@options["mime_map"]}" if @options["mime_map"]
43
- argv << "-r #{@options["docroot"]}" if @options["docroot"]
44
- argv << "-n #{@options["num_procs"]}" if @options["num_procs"]
45
- argv << "-B" if @options["debug"]
46
- argv << "-S #{@options["config_script"]}" if @options["config_script"]
47
- argv << "--user #{@options["user"]}" if @options["user"]
48
- argv << "--group #{@options["group"]}" if @options["group"]
49
- argv << "--prefix #{@options["prefix"]}" if @options["prefix"]
50
- cmd = argv.join " "
51
-
52
- puts cmd if @verbose
53
- output = `#{cmd}`
54
- unless $?.success?
55
- puts cmd unless @verbose
56
- puts output
57
- end
17
+ def read_options
18
+ @options = {
19
+ "environment" => ENV['RAILS_ENV'] || "development",
20
+ "port" => 3000,
21
+ "pid_file" => "tmp/pids/mongrel.pid",
22
+ "log_file" => "log/mongrel.log",
23
+ "servers" => 2
24
+ }
25
+ conf = YAML.load_file(@config_file)
26
+ @options.merge! conf if conf
27
+
28
+ process_pid_file @options["pid_file"]
29
+ process_log_file @options["log_file"]
30
+
31
+ start_port = end_port = @only
32
+ start_port ||= @options["port"].to_i
33
+ end_port ||= start_port + @options["servers"] - 1
34
+ @ports = (start_port..end_port).to_a
35
+ end
36
+
37
+ def process_pid_file(pid_file)
38
+ @pid_file_ext = File.extname(pid_file)
39
+ @pid_file_base = File.basename(pid_file, @pid_file_ext)
40
+ @pid_file_dir = File.dirname(pid_file)
41
+ end
42
+
43
+ def process_log_file(log_file)
44
+ @log_file_ext = File.extname(log_file)
45
+ @log_file_base = File.basename(log_file, @log_file_ext)
46
+ @log_file_dir = File.dirname(log_file)
47
+ end
48
+
49
+ def port_pid_file(port)
50
+ pid_file = [@pid_file_base, port].join(".") + @pid_file_ext
51
+ File.join(@pid_file_dir, pid_file)
52
+ end
53
+
54
+ def port_log_file(port)
55
+ log_file = [@log_file_base, port].join(".") + @log_file_ext
56
+ File.join(@log_file_dir, log_file)
57
+ end
58
+
59
+ def start
60
+ read_options
61
+
62
+ argv = [ "mongrel_rails" ]
63
+ argv << "start"
64
+ argv << "-d"
65
+ argv << "-e #{@options["environment"]}" if @options["environment"]
66
+ argv << "-a #{@options["address"]}" if @options["address"]
67
+ argv << "-c #{@options["cwd"]}" if @options["cwd"]
68
+ argv << "-t #{@options["timeout"]}" if @options["timeout"]
69
+ argv << "-m #{@options["mime_map"]}" if @options["mime_map"]
70
+ argv << "-r #{@options["docroot"]}" if @options["docroot"]
71
+ argv << "-n #{@options["num_procs"]}" if @options["num_procs"]
72
+ argv << "-B" if @options["debug"]
73
+ argv << "-S #{@options["config_script"]}" if @options["config_script"]
74
+ argv << "--user #{@options["user"]}" if @options["user"]
75
+ argv << "--group #{@options["group"]}" if @options["group"]
76
+ argv << "--prefix #{@options["prefix"]}" if @options["prefix"]
77
+ cmd = argv.join " "
78
+
79
+ @ports.each do |port|
80
+ if @clean && pid_file_exists?(port) && !check_process(port)
81
+ pid_file = port_pid_file(port)
82
+ log "missing process: removing #{pid_file}"
83
+ File.unlink(pid_file)
58
84
  end
85
+
86
+ if pid_file_exists?(port) && check_process(port)
87
+ log "already started port #{port}"
88
+ next
89
+ end
90
+
91
+ exec_cmd = cmd + " -p #{port} -P #{port_pid_file(port)}"
92
+ exec_cmd += " -l #{port_log_file(port)}"
93
+ log "starting port #{port}"
94
+ log_verbose exec_cmd
95
+ output = `#{exec_cmd}`
96
+ log_error output unless $?.success?
59
97
  end
98
+ end
60
99
 
61
- def stop
62
- read_options
63
- port = @options["port"].to_i - 1
64
- pid = @options["pid_file"].split(".")
65
- puts "Stopping #{@options["servers"]} Mongrel servers..."
66
- 1.upto(@options["servers"].to_i) do |i|
67
- argv = [ "mongrel_rails" ]
68
- argv << "stop"
69
- argv << "-P #{pid[0]}.#{port+i}.#{pid[1]}"
70
- argv << "-c #{@options["cwd"]}" if @options["cwd"]
71
- argv << "-f" if @force
72
- cmd = argv.join " "
73
- puts cmd if @verbose
74
- output = `#{cmd}`
75
- unless $?.success?
76
- puts cmd unless @verbose
77
- puts output
78
- end
100
+ def stop
101
+ read_options
102
+
103
+ argv = [ "mongrel_rails" ]
104
+ argv << "stop"
105
+ argv << "-c #{@options["cwd"]}" if @options["cwd"]
106
+ argv << "-f" if @force
107
+ cmd = argv.join " "
108
+
109
+ @ports.each do |port|
110
+ pid = check_process(port)
111
+ if @clean && pid && !pid_file_exists?(port)
112
+ log "missing pid_file: killing mongrel_rails port #{port}, pid #{pid}"
113
+ Process.kill("KILL", pid.to_i)
114
+ end
115
+
116
+ if !check_process(port)
117
+ log "already stopped port #{port}"
118
+ next
79
119
  end
120
+
121
+ exec_cmd = cmd + " -P #{port_pid_file(port)}"
122
+ log "stopping port #{port}"
123
+ log_verbose exec_cmd
124
+ output = `#{exec_cmd}`
125
+ log_error output unless $?.success?
126
+
80
127
  end
128
+ end
129
+
130
+ def status
131
+ read_options
81
132
 
133
+ status = STATUS_OK
134
+
135
+ @ports.each do |port|
136
+ pid = check_process(port)
137
+ unless pid_file_exists?(port)
138
+ log "missing pid_file: #{port_pid_file(port)}"
139
+ status = STATUS_ERROR
140
+ else
141
+ log "found pid_file: #{port_pid_file(port)}"
142
+ end
143
+ if pid
144
+ log "found mongrel_rails: port #{port}, pid #{pid}"
145
+ else
146
+ log "missing mongrel_rails: port #{port}"
147
+ status = STATUS_ERROR
148
+ end
149
+ puts ""
150
+ end
151
+
152
+ return status
153
+ end
154
+
155
+ def pid_file_exists?(port)
156
+ pid_file = port_pid_file(port)
157
+ exists = false
158
+ chdir_cwd do
159
+ exists = File.exists?(pid_file)
160
+ end
161
+ exists
162
+ end
163
+
164
+ def check_process(port)
165
+ if pid_file_exists?(port)
166
+ pid = read_pid(port)
167
+ ps_output = `ps -o #{cmd_name}= -p #{pid}`
168
+ pid = ps_output =~ /mongrel_rails/ ? pid : nil
169
+ else
170
+ pid = find_pid(port)
171
+ end
172
+ return pid
173
+ end
174
+
175
+ def cmd_name
176
+ RUBY_PLATFORM =~ /solaris/i ? "args" : "command"
177
+ end
178
+
179
+ def chdir_cwd
180
+ pwd = Dir.pwd
181
+ Dir.chdir(@options["cwd"]) if @options["cwd"]
182
+ yield
183
+ Dir.chdir(pwd) if @options["cwd"]
184
+ end
185
+
186
+ def read_pid(port)
187
+ pid_file = port_pid_file(port)
188
+ pid = 0
189
+ chdir_cwd do
190
+ pid = File.read(pid_file)
191
+ end
192
+ return pid
193
+ end
194
+
195
+ def find_pid(port)
196
+ ps_cmd = "ps -ewwo pid,#{cmd_name}"
197
+ ps_output = `#{ps_cmd}`
198
+ ps_output.each do |line|
199
+ if line =~ /-P #{Regexp.escape(port_pid_file(port))} /
200
+ pid = line.split[0]
201
+ return pid
202
+ end
203
+ end
204
+ return nil
205
+ end
206
+
207
+ def log_error(message)
208
+ log(message)
209
+ end
210
+
211
+ def log_verbose(message)
212
+ log(message) if @verbose
213
+ end
214
+
215
+ def log(message)
216
+ puts message
217
+ end
82
218
  end
83
-
84
219
  class Start < GemPlugin::Plugin "/commands"
85
220
  include ExecBase
86
221
 
87
222
  def configure
88
223
  options [
89
224
  ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
90
- ['-v', '--verbose', "Print all called commands and output.", :@verbose, false]
225
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
226
+ ['', '--clean', "Remove pid_file if needed before starting", :@clean, false],
227
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
91
228
  ]
92
229
  end
93
230
 
@@ -103,7 +240,9 @@ module Cluster
103
240
  options [
104
241
  ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
105
242
  ['-f', '--force', "Force the shutdown.", :@force, false],
106
- ['-v', '--verbose', "Print all called commands and output.", :@verbose, false]
243
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
244
+ ['', '--clean', "Remove orphaned process if needed before stopping", :@clean, false],
245
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
107
246
  ]
108
247
  end
109
248
 
@@ -119,7 +258,9 @@ module Cluster
119
258
  options [
120
259
  ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
121
260
  ['-f', '--force', "Force the shutdown.", :@force, false],
122
- ['-v', '--verbose', "Print all called commands and output.", :@verbose, false]
261
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
262
+ ['', '--clean', "Call stop and start with --clean", :@clean, false],
263
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
123
264
  ]
124
265
  end
125
266
 
@@ -131,15 +272,15 @@ module Cluster
131
272
  end
132
273
 
133
274
  class Configure < GemPlugin::Plugin "/commands"
134
- include Mongrel::Command::Base
275
+ include ExecBase
135
276
 
136
277
  def configure
137
278
  options [
138
279
  ["-e", "--environment ENV", "Rails environment to run as", :@environment, nil],
139
280
  ['-p', '--port PORT', "Starting port to bind to", :@port, 3000],
140
281
  ['-a', '--address ADDR', "Address to bind to", :@address, nil],
141
- ['-l', '--log FILE', "Where to write log messages", :@log_file, nil],
142
- ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
282
+ ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
283
+ ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "tmp/pids/mongrel.pid"],
143
284
  ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, nil],
144
285
  ['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, nil],
145
286
  ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
@@ -185,9 +326,26 @@ module Cluster
185
326
  @options["group"] = @group if @group
186
327
  @options["prefix"] = @prefix if @prefix
187
328
 
188
- puts "Writing configuration file to #{@config_file}."
329
+ log "Writing configuration file to #{@config_file}."
189
330
  File.open(@config_file,"w") {|f| f.write(@options.to_yaml)}
190
331
  end
191
332
  end
333
+
334
+ class Status < GemPlugin::Plugin "/commands"
335
+ include ExecBase
336
+
337
+ def configure
338
+ options [
339
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
340
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
341
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
342
+ ]
343
+ end
344
+
345
+ def run
346
+ status
347
+ end
348
+
349
+ end
192
350
  end
193
351
 
@@ -0,0 +1,351 @@
1
+ require 'gem_plugin'
2
+ require 'mongrel'
3
+ require 'yaml'
4
+
5
+ module Cluster
6
+ module ExecBase
7
+ include Mongrel::Command::Base
8
+
9
+ STATUS_OK = 0
10
+ STATUS_ERROR = 2
11
+
12
+ def validate
13
+ valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.")
14
+ return @valid
15
+ end
16
+
17
+ def read_options
18
+ @options = {
19
+ "environment" => ENV['RAILS_ENV'] || "development",
20
+ "port" => 3000,
21
+ "pid_file" => "tmp/pids/mongrel.pid",
22
+ "log_file" => "log/mongrel.log",
23
+ "servers" => 2
24
+ }
25
+ conf = YAML.load_file(@config_file)
26
+ @options.merge! conf if conf
27
+
28
+ process_pid_file @options["pid_file"]
29
+ process_log_file @options["log_file"]
30
+
31
+ start_port = end_port = @only
32
+ start_port ||= @options["port"].to_i
33
+ end_port ||= start_port + @options["servers"] - 1
34
+ @ports = (start_port..end_port).to_a
35
+ end
36
+
37
+ def process_pid_file(pid_file)
38
+ @pid_file_ext = File.extname(pid_file)
39
+ @pid_file_base = File.basename(pid_file, @pid_file_ext)
40
+ @pid_file_dir = File.dirname(pid_file)
41
+ end
42
+
43
+ def process_log_file(log_file)
44
+ @log_file_ext = File.extname(log_file)
45
+ @log_file_base = File.basename(log_file, @log_file_ext)
46
+ @log_file_dir = File.dirname(log_file)
47
+ end
48
+
49
+ def port_pid_file(port)
50
+ pid_file = [@pid_file_base, port].join(".") + @pid_file_ext
51
+ File.join(@pid_file_dir, pid_file)
52
+ end
53
+
54
+ def port_log_file(port)
55
+ log_file = [@log_file_base, port].join(".") + @log_file_ext
56
+ File.join(@log_file_dir, log_file)
57
+ end
58
+
59
+ def start
60
+ read_options
61
+
62
+ argv = [ "mongrel_rails" ]
63
+ argv << "start"
64
+ argv << "-d"
65
+ argv << "-e #{@options["environment"]}" if @options["environment"]
66
+ argv << "-a #{@options["address"]}" if @options["address"]
67
+ argv << "-c #{@options["cwd"]}" if @options["cwd"]
68
+ argv << "-t #{@options["timeout"]}" if @options["timeout"]
69
+ argv << "-m #{@options["mime_map"]}" if @options["mime_map"]
70
+ argv << "-r #{@options["docroot"]}" if @options["docroot"]
71
+ argv << "-n #{@options["num_procs"]}" if @options["num_procs"]
72
+ argv << "-B" if @options["debug"]
73
+ argv << "-S #{@options["config_script"]}" if @options["config_script"]
74
+ argv << "--user #{@options["user"]}" if @options["user"]
75
+ argv << "--group #{@options["group"]}" if @options["group"]
76
+ argv << "--prefix #{@options["prefix"]}" if @options["prefix"]
77
+ cmd = argv.join " "
78
+
79
+ @ports.each do |port|
80
+ if @clean && pid_file_exists?(port) && !check_process(port)
81
+ pid_file = port_pid_file(port)
82
+ log "missing process: removing #{pid_file}"
83
+ File.unlink(pid_file)
84
+ end
85
+
86
+ if pid_file_exists?(port) && check_process(port)
87
+ log "already started port #{port}"
88
+ next
89
+ end
90
+
91
+ exec_cmd = cmd + " -p #{port} -P #{port_pid_file(port)}"
92
+ exec_cmd += " -l #{port_log_file(port)}"
93
+ log "starting port #{port}"
94
+ log_verbose exec_cmd
95
+ output = `#{exec_cmd}`
96
+ log_error output unless $?.success?
97
+ end
98
+ end
99
+
100
+ def stop
101
+ read_options
102
+
103
+ argv = [ "mongrel_rails" ]
104
+ argv << "stop"
105
+ argv << "-c #{@options["cwd"]}" if @options["cwd"]
106
+ argv << "-f" if @force
107
+ cmd = argv.join " "
108
+
109
+ @ports.each do |port|
110
+ pid = check_process(port)
111
+ if @clean && pid && !pid_file_exists?(port)
112
+ log "missing pid_file: killing mongrel_rails port #{port}, pid #{pid}"
113
+ Process.kill("KILL", pid.to_i)
114
+ end
115
+
116
+ if !check_process(port)
117
+ log "already stopped port #{port}"
118
+ next
119
+ end
120
+
121
+ exec_cmd = cmd + " -P #{port_pid_file(port)}"
122
+ log "stopping port #{port}"
123
+ log_verbose exec_cmd
124
+ output = `#{exec_cmd}`
125
+ log_error output unless $?.success?
126
+
127
+ end
128
+ end
129
+
130
+ def status
131
+ read_options
132
+
133
+ status = STATUS_OK
134
+
135
+ @ports.each do |port|
136
+ pid = check_process(port)
137
+ unless pid_file_exists?(port)
138
+ log "missing pid_file: #{port_pid_file(port)}"
139
+ status = STATUS_ERROR
140
+ else
141
+ log "found pid_file: #{port_pid_file(port)}"
142
+ end
143
+ if pid
144
+ log "found mongrel_rails: port #{port}, pid #{pid}"
145
+ else
146
+ log "missing mongrel_rails: port #{port}"
147
+ status = STATUS_ERROR
148
+ end
149
+ puts ""
150
+ end
151
+
152
+ return status
153
+ end
154
+
155
+ def pid_file_exists?(port)
156
+ pid_file = port_pid_file(port)
157
+ exists = false
158
+ chdir_cwd do
159
+ exists = File.exists?(pid_file)
160
+ end
161
+ exists
162
+ end
163
+
164
+ def check_process(port)
165
+ if pid_file_exists?(port)
166
+ pid = read_pid(port)
167
+ ps_output = `ps -o #{cmd_name}= -p #{pid}`
168
+ pid = ps_output =~ /mongrel_rails/ ? pid : nil
169
+ else
170
+ pid = find_pid(port)
171
+ end
172
+ return pid
173
+ end
174
+
175
+ def cmd_name
176
+ RUBY_PLATFORM =~ /solaris/i ? "args" : "command"
177
+ end
178
+
179
+ def chdir_cwd
180
+ pwd = Dir.pwd
181
+ Dir.chdir(@options["cwd"]) if @options["cwd"]
182
+ yield
183
+ Dir.chdir(pwd) if @options["cwd"]
184
+ end
185
+
186
+ def read_pid(port)
187
+ pid_file = port_pid_file(port)
188
+ pid = 0
189
+ chdir_cwd do
190
+ pid = File.read(pid_file)
191
+ end
192
+ return pid
193
+ end
194
+
195
+ def find_pid(port)
196
+ ps_cmd = "ps -ewwo pid,#{cmd_name}"
197
+ ps_output = `#{ps_cmd}`
198
+ ps_output.each do |line|
199
+ if line =~ /-P #{Regexp.escape(port_pid_file(port))} /
200
+ pid = line.split[0]
201
+ return pid
202
+ end
203
+ end
204
+ return nil
205
+ end
206
+
207
+ def log_error(message)
208
+ log(message)
209
+ end
210
+
211
+ def log_verbose(message)
212
+ log(message) if @verbose
213
+ end
214
+
215
+ def log(message)
216
+ puts message
217
+ end
218
+ end
219
+ class Start < GemPlugin::Plugin "/commands"
220
+ include ExecBase
221
+
222
+ def configure
223
+ options [
224
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
225
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
226
+ ['', '--clean', "Remove pid_file if needed before starting", :@clean, false],
227
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
228
+ ]
229
+ end
230
+
231
+ def run
232
+ start
233
+ end
234
+ end
235
+
236
+ class Stop < GemPlugin::Plugin "/commands"
237
+ include ExecBase
238
+
239
+ def configure
240
+ options [
241
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
242
+ ['-f', '--force', "Force the shutdown.", :@force, false],
243
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
244
+ ['', '--clean', "Remove orphaned process if needed before stopping", :@clean, false],
245
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
246
+ ]
247
+ end
248
+
249
+ def run
250
+ stop
251
+ end
252
+ end
253
+
254
+ class Restart < GemPlugin::Plugin "/commands"
255
+ include ExecBase
256
+
257
+ def configure
258
+ options [
259
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
260
+ ['-f', '--force', "Force the shutdown.", :@force, false],
261
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
262
+ ['', '--clean', "Call stop and start with --clean", :@clean, false],
263
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
264
+ ]
265
+ end
266
+
267
+ def run
268
+ stop
269
+ start
270
+ end
271
+
272
+ end
273
+
274
+ class Configure < GemPlugin::Plugin "/commands"
275
+ include ExecBase
276
+
277
+ def configure
278
+ options [
279
+ ["-e", "--environment ENV", "Rails environment to run as", :@environment, nil],
280
+ ['-p', '--port PORT', "Starting port to bind to", :@port, 3000],
281
+ ['-a', '--address ADDR', "Address to bind to", :@address, nil],
282
+ ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
283
+ ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "tmp/pids/mongrel.pid"],
284
+ ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, nil],
285
+ ['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, nil],
286
+ ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
287
+ ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, nil],
288
+ ['-n', '--num-procs INT', "Number of processor threads to use", :@num_procs, nil],
289
+ ['-B', '--debug', "Enable debugging mode", :@debug, nil],
290
+ ['-S', '--script PATH', "Load the given file as an extra config script.", :@config_script, nil],
291
+ ['-N', '--num-servers INT', "Number of Mongrel servers", :@servers, 2],
292
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
293
+ ['', '--user USER', "User to run as", :@user, nil],
294
+ ['', '--group GROUP', "Group to run as", :@group, nil],
295
+ ['', '--prefix PREFIX', "Rails prefix to use", :@prefix, nil]
296
+ ]
297
+ end
298
+
299
+ def validate
300
+ @servers = @servers.to_i
301
+
302
+ valid?(@servers > 0, "Must give a valid number of servers")
303
+ valid_dir? File.dirname(@config_file), "Path to config file not valid: #{@config_file}"
304
+
305
+ return @valid
306
+ end
307
+
308
+ def run
309
+ @options = {
310
+ "port" => @port,
311
+ "servers" => @servers,
312
+ "pid_file" => @pid_file
313
+ }
314
+
315
+ @options["log_file"] = @log_file if @log_file
316
+ @options["debug"] = @debug if @debug
317
+ @options["num_procs"] = @num_procs if @num_procs
318
+ @options["docroot"] = @docroot if @docroots
319
+ @options["address"] = @address if @address
320
+ @options["timeout"] = @timeout if @timeout
321
+ @options["environment"] = @environment if @environment
322
+ @options["mime_map"] = @mime_map if @mime_map
323
+ @options["config_script"] = @config_script if @config_script
324
+ @options["cwd"] = @cwd if @cwd
325
+ @options["user"] = @user if @user
326
+ @options["group"] = @group if @group
327
+ @options["prefix"] = @prefix if @prefix
328
+
329
+ log "Writing configuration file to #{@config_file}."
330
+ File.open(@config_file,"w") {|f| f.write(@options.to_yaml)}
331
+ end
332
+ end
333
+
334
+ class Status < GemPlugin::Plugin "/commands"
335
+ include ExecBase
336
+
337
+ def configure
338
+ options [
339
+ ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
340
+ ['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
341
+ ['', '--only PORT', "Port number of cluster member", :@only, nil]
342
+ ]
343
+ end
344
+
345
+ def run
346
+ status
347
+ end
348
+
349
+ end
350
+ end
351
+
@@ -7,7 +7,12 @@ Capistrano.configuration(:must_exist).load do
7
7
  set :mongrel_user, nil
8
8
  set :mongrel_group, nil
9
9
  set :mongrel_prefix, nil
10
-
10
+ set :mongrel_rails, 'mongrel_rails'
11
+ set :mongrel_clean, false
12
+ set :mongrel_pid_file, nil
13
+ set :mongrel_log_file, nil
14
+ set :mongrel_config_script, nil
15
+
11
16
  desc <<-DESC
12
17
  Configure Mongrel processes on the app server. This uses the :use_sudo
13
18
  variable to determine whether to use sudo or not. By default, :use_sudo is
@@ -17,16 +22,19 @@ Capistrano.configuration(:must_exist).load do
17
22
  set_mongrel_conf
18
23
 
19
24
  argv = []
20
- argv << "mongrel_rails cluster::configure"
25
+ argv << "#{mongrel_rails} cluster::configure"
21
26
  argv << "-N #{mongrel_servers.to_s}"
22
27
  argv << "-p #{mongrel_port.to_s}"
23
28
  argv << "-e #{mongrel_environment}"
24
29
  argv << "-a #{mongrel_address}"
25
30
  argv << "-c #{current_path}"
26
31
  argv << "-C #{mongrel_conf}"
32
+ argv << "-P #{mongrel_pid_file}" if mongrel_pid_file
33
+ argv << "-l #{mongrel_log_file}" if mongrel_log_file
27
34
  argv << "--user #{mongrel_user}" if mongrel_user
28
35
  argv << "--group #{mongrel_group}" if mongrel_group
29
36
  argv << "--prefix #{mongrel_prefix}" if mongrel_prefix
37
+ argv << "-S #{mongrel_config_script}" if mongrel_config_script
30
38
  cmd = argv.join " "
31
39
  send(run_method, cmd)
32
40
  end
@@ -37,7 +45,9 @@ Capistrano.configuration(:must_exist).load do
37
45
  DESC
38
46
  task :start_mongrel_cluster , :roles => :app do
39
47
  set_mongrel_conf
40
- send(run_method, "mongrel_rails cluster::start -C #{mongrel_conf}")
48
+ cmd = "#{mongrel_rails} cluster::start -C #{mongrel_conf}"
49
+ cmd += " --clean" if mongrel_clean
50
+ send(run_method, cmd)
41
51
  end
42
52
 
43
53
  desc <<-DESC
@@ -46,7 +56,9 @@ Capistrano.configuration(:must_exist).load do
46
56
  DESC
47
57
  task :restart_mongrel_cluster , :roles => :app do
48
58
  set_mongrel_conf
49
- send(run_method, "mongrel_rails cluster::restart -C #{mongrel_conf}")
59
+ cmd = "#{mongrel_rails} cluster::restart -C #{mongrel_conf}"
60
+ cmd += " --clean" if mongrel_clean
61
+ send(run_method, cmd)
50
62
  end
51
63
 
52
64
  desc <<-DESC
@@ -56,7 +68,19 @@ Capistrano.configuration(:must_exist).load do
56
68
  DESC
57
69
  task :stop_mongrel_cluster , :roles => :app do
58
70
  set_mongrel_conf
59
- send(run_method, "mongrel_rails cluster::stop -C #{mongrel_conf}")
71
+ cmd = "#{mongrel_rails} cluster::stop -C #{mongrel_conf}"
72
+ cmd += " --clean" if mongrel_clean
73
+ send(run_method, cmd)
74
+ end
75
+
76
+ desc <<-DESC
77
+ Check the status of the Mongrel processes on the app server. This uses the :use_sudo
78
+ variable to determine whether to use sudo or not. By default, :use_sudo is
79
+ set to true.
80
+ DESC
81
+ task :status_mongrel_cluster , :roles => :app do
82
+ set_mongrel_conf
83
+ send(run_method, "#{mongrel_rails} cluster::status -C #{mongrel_conf}")
60
84
  end
61
85
 
62
86
  desc <<-DESC
@@ -77,4 +101,4 @@ Capistrano.configuration(:must_exist).load do
77
101
  set :mongrel_conf, "/etc/mongrel_cluster/#{application}.yml" unless mongrel_conf
78
102
  end
79
103
 
80
- end
104
+ end
@@ -1,6 +1,6 @@
1
1
  #!/bin/bash
2
2
  #
3
- # Copyright (c) 2006 Bradley Taylor, bradley@railsmachine.com
3
+ # Copyright (c) 2007 Bradley Taylor, bradley@railsmachine.com
4
4
  #
5
5
  # mongrel_cluster Startup script for Mongrel clusters.
6
6
  #
@@ -10,8 +10,10 @@
10
10
  #
11
11
 
12
12
  CONF_DIR=/etc/mongrel_cluster
13
- RETVAL=0
13
+ PID_DIR=/var/run/mongrel_cluster
14
+ USER=mongrel
14
15
 
16
+ RETVAL=0
15
17
 
16
18
  # Gracefully exit if the controller is missing.
17
19
  which mongrel_cluster_ctl >/dev/null || exit 0
@@ -21,6 +23,10 @@ which mongrel_cluster_ctl >/dev/null || exit 0
21
23
 
22
24
  case "$1" in
23
25
  start)
26
+ # Create pid directory
27
+ mkdir -p $PID_DIR
28
+ chown $USER:$USER $PID_DIR
29
+
24
30
  mongrel_cluster_ctl start -c $CONF_DIR
25
31
  RETVAL=$?
26
32
  ;;
@@ -31,11 +37,15 @@ case "$1" in
31
37
  restart)
32
38
  mongrel_cluster_ctl restart -c $CONF_DIR
33
39
  RETVAL=$?
40
+ ;;
41
+ status)
42
+ mongrel_cluster_ctl status -c $CONF_DIR
43
+ RETVAL=$?
34
44
  ;;
35
45
  *)
36
- echo "Usage: mongrel_cluster {start|stop|restart}"
46
+ echo "Usage: mongrel_cluster {start|stop|restart|status}"
37
47
  exit 1
38
48
  ;;
39
49
  esac
40
50
 
41
- exit $RETVAL
51
+ exit $RETVAL
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: mongrel_cluster
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2006-09-11 00:00:00 -07:00
6
+ version: 1.0.2
7
+ date: 2007-07-10 00:00:00 -04:00
8
8
  summary: Mongrel plugin that provides commands and Capistrano tasks for managing multiple Mongrel processes.
9
9
  require_paths:
10
10
  - lib
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Bradley Taylor
30
31
  files:
@@ -35,6 +36,7 @@ files:
35
36
  - bin/mongrel_cluster_ctl
36
37
  - lib/mongrel_cluster
37
38
  - lib/mongrel_cluster/recipes.rb
39
+ - lib/mongrel_cluster/init.rb~
38
40
  - lib/mongrel_cluster/init.rb
39
41
  - tools/rakehelp.rb
40
42
  - resources/mongrel_cluster
@@ -59,7 +61,7 @@ dependencies:
59
61
  requirements:
60
62
  - - ">="
61
63
  - !ruby/object:Gem::Version
62
- version: 0.2.1
64
+ version: 0.2.2
63
65
  version:
64
66
  - !ruby/object:Gem::Dependency
65
67
  name: mongrel
@@ -68,5 +70,5 @@ dependencies:
68
70
  requirements:
69
71
  - - ">="
70
72
  - !ruby/object:Gem::Version
71
- version: 0.3.13.4
73
+ version: 1.0.1
72
74
  version: