好东西,可以这样做集群
Cool!
Here's a quick rake task to make a thin cluster:
rake thin:cluster:start
rake thin:cluster:stop
For the start task, you can pass in the RAILS_ENV and the SIZE of the cluster (default 4).
rake thin:cluster:start RAILS_ENV=production SIZE=10
namespace :thin do
namespace :cluster do
desc 'Start thin cluster'
task :start => :environment do
`cd #{RAILS_ROOT}`
port_range = RAILS_ENV == 'development' ? 3 : 8
(ENV['SIZE'] ? ENV['SIZE'].to_i : 4).times do |i|
Thread.new do
port = "#{port_range}%03d" % i
str = "thin start -d -p#{port} -Ptmp/pids/thin-#{port}.pid"
str += " -e#{RAILS_ENV}"
puts "Starting server on port #{port}..."
`#{str}`
end
end
end
desc 'Stop thin cluster'
task :stop => :environment do
`cd #{RAILS_ROOT}`
port_range = RAILS_ENV == 'development' ? 3 : 8
Dir.new("#{RAILS_ROOT}/tmp/pids").each do |file|
Thread.new do
if file.starts_with?("thin-#{port_range}")
str = "thin stop -Ptmp/pids/#{file}"
puts "Stopping server on port #{file[/\d+/]}..."
`#{str}`
end
end
end
end
end
end