1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

changed clean to cleanup a messy cluster, misc fixes

git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@524 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
bktaylor 2007-02-22 04:10:34 +00:00
parent 3f82dfd23b
commit 85a123bfb3
3 changed files with 205 additions and 131 deletions

View file

@ -15,7 +15,7 @@ setup_rdoc ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc']
desc "Does a full compile, test run"
task :default => [:test, :package]
version="0.2.2"
version="1.0.1.1"
name="mongrel_cluster"
setup_gem(name, version) do |spec|

View file

@ -3,136 +3,201 @@ require 'mongrel'
require 'yaml'
module Cluster
module ExecBase
include Mongrel::Command::Base
include Mongrel::Command::Base
STATUS_OK = 0
STATUS_MISSING = 2
def validate
valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.")
return @valid
end
def read_options
@options = {
"environment" => ENV['RAILS_ENV'] || "development",
"port" => 3000,
"pid_file" => "log/mongrel.pid",
"servers" => 2
}
conf = YAML.load_file(@config_file)
@options.merge! conf if conf
@pid_file = @options["pid_file"].split(".")
start_port = end_port = @only
start_port ||= @options["port"].to_i
end_port ||= start_port + @options["servers"] - 1
@ports = (start_port..end_port).to_a
end
def port_pid_file(port)
"#{@pid_file[0]}.#{port}.#{@pid_file[1]}"
end
def start
read_options
argv = [ "mongrel_rails" ]
argv << "start"
argv << "-d"
argv << "-e #{@options["environment"]}" if @options["environment"]
argv << "-a #{@options["address"]}" if @options["address"]
argv << "-l #{@options["log_file"]}" if @options["log_file"]
argv << "-c #{@options["cwd"]}" if @options["cwd"]
argv << "-t #{@options["timeout"]}" if @options["timeout"]
argv << "-m #{@options["mime_map"]}" if @options["mime_map"]
argv << "-r #{@options["docroot"]}" if @options["docroot"]
argv << "-n #{@options["num_procs"]}" if @options["num_procs"]
argv << "-B" if @options["debug"]
argv << "-S #{@options["config_script"]}" if @options["config_script"]
argv << "--user #{@options["user"]}" if @options["user"]
argv << "--group #{@options["group"]}" if @options["group"]
argv << "--prefix #{@options["prefix"]}" if @options["prefix"]
cmd = argv.join " "
puts "Starting #{@ports.length} Mongrel servers..."
@ports.each do |port|
cmd += " -p #{port} -P #{port_pid_file(port)}"
puts cmd if @verbose
output = `#{cmd}`
unless $?.success?
puts cmd unless @verbose
puts output
end
end
end
def stop
read_options
argv = [ "mongrel_rails" ]
argv << "stop"
argv << "-c #{@options["cwd"]}" if @options["cwd"]
argv << "-f" if @force
cmd = argv.join " "
puts "Stopping #{@ports.length} Mongrel servers..."
@ports.each do |port|
cmd += " -P #{port_pid_file(port)}"
puts cmd if @verbose
output = `#{cmd}`
unless $?.success?
puts cmd unless @verbose
puts output
end
end
end
def clean
read_options
puts "Cleaning #{@ports.length} pid files..."
Dir.chdir @options["cwd"] if @options["cwd"]
@ports.each do |port|
pid_file = port_pid_file(port)
puts "Removing #{pid_file}!" if @verbose
File.unlink(pid_file) if File.exists?(pid_file)
end
end
STATUS_OK = 0
STATUS_ERROR = 2
def status
read_options
def validate
valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.")
return @valid
end
def read_options
@options = {
"environment" => ENV['RAILS_ENV'] || "development",
"port" => 3000,
"pid_file" => "tmp/pids/mongrel.pid",
"log_file" => "log/mongrel.log",
"servers" => 2
}
conf = YAML.load_file(@config_file)
@options.merge! conf if conf
puts "Checking #{@ports.length} Mongrel servers..."
Dir.chdir @options["cwd"] if @options["cwd"]
process_pid_file @options["pid_file"]
process_log_file @options["log_file"]
status = STATUS_OK
@ports.each do |port|
pid_file = port_pid_file(port)
if File.exists?(pid_file)
pid = File.read(pid_file)
ps_output = `ps -o command= -p #{pid}`
status = STATUS_MISSING unless (ps_output =~ /mongrel_rails/)
msg = status == STATUS_OK ? "Found dog: #{port}" : "Lost dog: #{port}"
else
msg = "Missing pid: #{pid_file}"
status = STATUS_MISSING
end
puts msg
start_port = end_port = @only
start_port ||= @options["port"].to_i
end_port ||= start_port + @options["servers"] - 1
@ports = (start_port..end_port).to_a
end
def process_pid_file(pid_file)
@pid_file_ext = File.extname(pid_file)
@pid_file_base = File.basename(pid_file, @pid_file_ext)
@pid_file_dir = File.dirname(pid_file)
end
def process_log_file(log_file)
@log_file_ext = File.extname(log_file)
@log_file_base = File.basename(log_file, @log_file_ext)
@log_file_dir = File.dirname(log_file)
end
def port_pid_file(port)
pid_file = [@pid_file_base, port].join(".") + @pid_file_ext
File.join(@pid_file_dir, pid_file)
end
def port_log_file(port)
log_file = [@log_file_base, port].join(".") + @log_file_ext
File.join(@log_file_dir, log_file)
end
def start
read_options
argv = [ "mongrel_rails" ]
argv << "start"
argv << "-d"
argv << "-e #{@options["environment"]}" if @options["environment"]
argv << "-a #{@options["address"]}" if @options["address"]
argv << "-c #{@options["cwd"]}" if @options["cwd"]
argv << "-t #{@options["timeout"]}" if @options["timeout"]
argv << "-m #{@options["mime_map"]}" if @options["mime_map"]
argv << "-r #{@options["docroot"]}" if @options["docroot"]
argv << "-n #{@options["num_procs"]}" if @options["num_procs"]
argv << "-B" if @options["debug"]
argv << "-S #{@options["config_script"]}" if @options["config_script"]
argv << "--user #{@options["user"]}" if @options["user"]
argv << "--group #{@options["group"]}" if @options["group"]
argv << "--prefix #{@options["prefix"]}" if @options["prefix"]
cmd = argv.join " "
@ports.each do |port|
pid_file = port_pid_file(port)
if @clean && pid_file_exists?(port) && !check_process(port)
log "Removing #{pid_file}!"
File.unlink(pid_file)
end
if pid_file_exists?(port) && check_process(port)
log "already started port #{port}"
next
end
return status
exec_cmd = cmd + " -p #{port} -P #{port_pid_file(port)}"
exec_cmd += " -l #{port_log_file(port)}"
log "starting port #{port}"
log_verbose exec_cmd
output = `#{exec_cmd}`
log_error output unless $?.success?
end
end
def stop
read_options
argv = [ "mongrel_rails" ]
argv << "stop"
argv << "-c #{@options["cwd"]}" if @options["cwd"]
argv << "-f" if @force
cmd = argv.join " "
@ports.each do |port|
pid = check_process(port)
if @clean && pid && !pid_file_exists?(port)
log "killing mongrel_rails (port: #{port}, pid:#{pid})"
Process.kill("KILL", pid.to_i)
end
if !check_process(port)
log "already stopped port #{port}"
next
end
exec_cmd = cmd + " -P #{port_pid_file(port)}"
log "stopping port #{port}"
log_verbose exec_cmd
output = `#{exec_cmd}`
log_error output unless $?.success?
end
end
def status
read_options
Dir.chdir @options["cwd"] if @options["cwd"]
status = STATUS_OK
@ports.each do |port|
pid = check_process(port)
unless pid_file_exists?(port)
log "missing pid_file: #{port_pid_file(port)}"
status = STATUS_ERROR
else
log "found pid_file: #{port_pid_file(port)}"
end
if pid
log "mongrel_rails (port: #{port}, pid:#{pid}) is running..."
else
log "mongrel_rails (port: #{port}) is not running..."
status = STATUS_ERROR
end
puts ""
end
return status
end
def pid_file_exists?(port)
pid_file = port_pid_file(port)
File.exists?(pid_file)
end
def check_process(port)
if pid_file_exists?(port)
pid = read_pid(port)
ps_output = `ps -o args= -p #{pid}`
pid = ps_output =~ /mongrel_rails/ ? pid : nil
else
pid = find_pid(port)
end
return pid
end
def read_pid(port)
pid_file = port_pid_file(port)
pid = File.read(pid_file)
end
def find_pid(port)
ps_cmd = "ps -ewwo pid,args"
ps_output = `#{ps_cmd}`
ps_output.each do |line|
if line =~ /-P #{Regexp.escape(port_pid_file(port))} /
pid = line.split[0]
return pid
end
end
return nil
end
def log_error(message)
log(message)
end
def log_verbose(message)
log(message) if @verbose
end
def log(message)
puts message
end
end
class Start < GemPlugin::Plugin "/commands"
include ExecBase
@ -140,13 +205,12 @@ module Cluster
options [
['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
['', '--clean', "Remove pid_file before starting", :@clean, false],
['', '--clean', "Remove pid_file if needed before starting", :@clean, false],
['', '--only PORT', "Port number of cluster member", :@only, nil]
]
end
def run
clean if @clean
start
end
end
@ -159,6 +223,7 @@ module Cluster
['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
['-f', '--force', "Force the shutdown.", :@force, false],
['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
['', '--clean', "Remove orphaned process if needed before stopping", :@clean, false],
['', '--only PORT', "Port number of cluster member", :@only, nil]
]
end
@ -176,6 +241,7 @@ module Cluster
['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"],
['-f', '--force', "Force the shutdown.", :@force, false],
['-v', '--verbose', "Print all called commands and output.", :@verbose, false],
['', '--clean', "Call stop and start with --clean", :@clean, false],
['', '--only PORT', "Port number of cluster member", :@only, nil]
]
end
@ -195,7 +261,7 @@ module Cluster
["-e", "--environment ENV", "Rails environment to run as", :@environment, nil],
['-p', '--port PORT', "Starting port to bind to", :@port, 3000],
['-a', '--address ADDR', "Address to bind to", :@address, nil],
['-l', '--log FILE', "Where to write log messages", :@log_file, nil],
['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "tmp/pids/mongrel.pid"],
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, nil],
['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, nil],
@ -242,7 +308,7 @@ module Cluster
@options["group"] = @group if @group
@options["prefix"] = @prefix if @prefix
puts "Writing configuration file to #{@config_file}."
log "Writing configuration file to #{@config_file}."
File.open(@config_file,"w") {|f| f.write(@options.to_yaml)}
end
end

View file

@ -7,7 +7,9 @@ Capistrano.configuration(:must_exist).load do
set :mongrel_user, nil
set :mongrel_group, nil
set :mongrel_prefix, nil
set :mongrel_rails, 'mongrel_rails'
set :mongrel_clean, false
desc <<-DESC
Configure Mongrel processes on the app server. This uses the :use_sudo
variable to determine whether to use sudo or not. By default, :use_sudo is
@ -17,7 +19,7 @@ Capistrano.configuration(:must_exist).load do
set_mongrel_conf
argv = []
argv << "mongrel_rails cluster::configure"
argv << "#{mongrel_rails} cluster::configure"
argv << "-N #{mongrel_servers.to_s}"
argv << "-p #{mongrel_port.to_s}"
argv << "-e #{mongrel_environment}"
@ -37,7 +39,9 @@ Capistrano.configuration(:must_exist).load do
DESC
task :start_mongrel_cluster , :roles => :app do
set_mongrel_conf
send(run_method, "mongrel_rails cluster::start -C #{mongrel_conf}")
cmd = "#{mongrel_rails} cluster::start -C #{mongrel_conf}"
cmd += " --clean" if mongrel_clean
send(run_method, cmd)
end
desc <<-DESC
@ -46,7 +50,9 @@ Capistrano.configuration(:must_exist).load do
DESC
task :restart_mongrel_cluster , :roles => :app do
set_mongrel_conf
send(run_method, "mongrel_rails cluster::restart -C #{mongrel_conf}")
cmd = "#{mongrel_rails} cluster::restart -C #{mongrel_conf}"
cmd += " --clean" if mongrel_clean
send(run_method, cmd)
end
desc <<-DESC
@ -56,7 +62,9 @@ Capistrano.configuration(:must_exist).load do
DESC
task :stop_mongrel_cluster , :roles => :app do
set_mongrel_conf
send(run_method, "mongrel_rails cluster::stop -C #{mongrel_conf}")
cmd = "#{mongrel_rails} cluster::stop -C #{mongrel_conf}"
cmd += " --clean" if mongrel_clean
send(run_method, cmd)
end
desc <<-DESC
@ -66,7 +74,7 @@ Capistrano.configuration(:must_exist).load do
DESC
task :status_mongrel_cluster , :roles => :app do
set_mongrel_conf
send(run_method, "mongrel_rails cluster::status -C #{mongrel_conf}")
send(run_method, "#{mongrel_rails} cluster::status -C #{mongrel_conf}")
end
desc <<-DESC