Brian Tol
unread,Jan 6, 2008, 2:43:41 PM1/6/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to thin-ruby
Here's a 'thin_cluster' script that works a lot like mongrel_rails
cluster::*. It can use existing mongrel_cluster.yml files, in fact.
FWIW, I thought I'd pass it along. Your millage will vary.
#!/usr/bin/env ruby
# <tt>thin_cluter start</tt>: Starts the Rails app in the current
directory with a cluster of thins.
# Run <tt>thin_cluter -h</tt> to get more usage.
require File.dirname(__FILE__) + '/../lib/thin'
require 'optparse'
options = {
'host' => '0.0.0.0',
'port' => 3000,
'env' => ENV['RAILS_ENV'] || "development",
'root' => Dir.pwd,
'log_file' => 'log/thin.log',
'pid_file' => 'tmp/pids/thin.pid',
'timeout' => 60,
'servers' => 2
}
cli_options = {}
config_filename = 'config/thin_cluster.yml'
opts = OptionParser.new do |opts|
opts.banner = "Usage: thin_cluster [options] start|stop|restart"
opts.separator ""
opts.separator "Cluster options:"
opts.on("-C", "--config", "Config file to use",
"(defaults to: #{config_filename})")
{ |user_config_filename|
config_filename = user_config_filename if File.exist?
(user_config_filename)
}
opts.on("-o", "--host HOST", "listen on HOST (default:
0.0.0.0)") { |host| cli_options['host'] = host }
opts.on("-p", "--port PORT", "use PORT (default:
3000)") { |port| cli_options['port'] = port }
opts.on("-e", "--env ENV", "Rails environment (default:
development)") { |env| cli_options['env'] = env }
opts.on("-c", "--chdir PATH", "listen on HOST (default: current
dir)") { |dir| cli_options['root'] = dir }
#opts.on("-d", "--daemonize", "Run daemonized in the
background") { cli_options['daemonize'] = true }
opts.on("-l", "--log-file FILE", "File to redirect output",
"(default:
#{options['log_file']})") { |file| cli_options['log_file'] = file }
opts.on("-P", "--pid-file FILE", "File to store PID",
"(default:
#{options['pid_file']})") { |file| cli_options['pid_file'] = file }
opts.on("-t", "--timeout SEC", "Request or command timeout in
sec",
"(default:
#{options['timeout']})") { |sec| cli_options['timeout'] = sec }
opts.on("-u", "--user NAME", "User to run daemon as (use with -
g)") { |user| cli_options['user'] = user }
opts.on("-g", "--group NAME", "Group to run daemon as (use with -
u)") { |group| cli_options['group'] = group }
opts.separator ""
opts.separator "Common options:"
opts.on_tail("-D", "--debug", "Set debbuging on") { $DEBUG = true }
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail('-v', '--version', "Show version") do
puts Thin::SERVER
exit
end
opts.parse! ARGV
end
if File.exist?(config_filename)
config_file_options = YAML.load_file(config_filename)
options.merge! config_file_options if config_file_options
end
options.merge! cli_options
def start_cluster(options)
port = options["port"].to_i - 1
pid = options["pid_file"].split(".")
puts "Starting #{options["servers"]} Thin servers..."
1.upto(options['servers'].to_i) do |i|
argv = [ 'thin' ]
argv << "start"
argv << "-d"
argv << "-e #{options["environment"]}" if options["environment"]
argv << "-p #{port+i}"
argv << "-o #{options["host"]}" if options["host"]
argv << "-l #{options["log_file"]}" if options["log_file"]
argv << "-P #{pid[0]}.#{port+i}.#{pid[1]}"
argv << "-c #{options["root"]}" if options["root"]
argv << "-t #{options["timeout"]}" if options["timeout"]
#argv << "-r #{options["root"]}" if options["root"]
#argv << "-n #{options["num_procs"]}" if options["num_procs"]
#argv << "-B" if options["debug"]
argv << "--user #{options["user"]}" if options["user"]
argv << "--group #{options["group"]}" if options["group"]
cmd = argv.join " "
puts cmd if $DEBUG
output = `#{cmd}`
unless $?.success?
puts cmd unless $DEBUG
puts output
end
end
end
def stop_cluster(options)
port = options["port"].to_i - 1
pid = options["pid_file"].split(".")
puts "Stopping #{options["servers"]} Thin servers..."
1.upto(options['servers'].to_i) do |i|
argv = [ "thin" ]
argv << "stop"
argv << "-P #{pid[0]}.#{port+i}.#{pid[1]}"
argv << "-c #{options["root"]}" if options["root"]
cmd = argv.join " "
puts cmd if $DEBUG
output = `#{cmd}`
unless $?.success?
puts cmd unless $DEBUG
puts output
end
end
end
case ARGV[0]
when 'start'
start_cluster(options)
when 'stop'
stop_cluster(options)
when 'restart'
stop_cluster(options)
start_cluster(options)
when nil
puts "Command required"
puts opts
exit 1
else
abort "Invalid command : #{ARGV[0]}"
end