Added option to script/process/spawner of specifying the binding address #5133 [zsombor]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5367 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2006-10-26 16:59:35 +00:00
parent b9ffbd3166
commit 464102d551
2 changed files with 58 additions and 23 deletions

@ -1,5 +1,7 @@
*SVN* *SVN*
* Added option to script/process/spawner of specifying the binding address #5133 [zsombor]
* Update environment.rb comments to include config.autoload_paths. Closes #6478 [caio] * Update environment.rb comments to include config.autoload_paths. Closes #6478 [caio]
* Update scaffold to use new form_tag block functionality. Closes #6480. [BobSilva] * Update scaffold to use new form_tag block functionality. Closes #6480. [BobSilva]

@ -23,15 +23,15 @@ def self.record_pid(name = "#{OPTIONS[:process]}.spawner", id = Process.pid)
def self.spawn_all def self.spawn_all
OPTIONS[:instances].times do |i| OPTIONS[:instances].times do |i|
port = OPTIONS[:port] + i port = OPTIONS[:port] + i
print "Checking if something is already running on port #{port}..." print "Checking if something is already running on #{OPTIONS[:address]}:#{port}..."
begin begin
srv = TCPServer.new('0.0.0.0', port) srv = TCPServer.new(OPTIONS[:address], port)
srv.close srv.close
srv = nil srv = nil
puts "NO" puts "NO"
puts "Starting dispatcher on port: #{port}" puts "Starting dispatcher on port: #{OPTIONS[:address]}:#{port}"
FileUtils.mkdir_p(OPTIONS[:pids]) FileUtils.mkdir_p(OPTIONS[:pids])
spawn(port) spawn(port)
@ -44,13 +44,25 @@ def self.spawn_all
class FcgiSpawner < Spawner class FcgiSpawner < Spawner
def self.spawn(port) def self.spawn(port)
system("#{OPTIONS[:spawner]} -f #{OPTIONS[:dispatcher]} -p #{port} -P #{OPTIONS[:pids]}/#{OPTIONS[:process]}.#{port}.pid") cmd = "#{OPTIONS[:spawner]} -f #{OPTIONS[:dispatcher]} -p #{port} -P #{OPTIONS[:pids]}/#{OPTIONS[:process]}.#{port}.pid"
cmd << " -a #{OPTIONS[:address]}" if can_bind_to_custom_address?
system(cmd)
end
def self.can_bind_to_custom_address?
@@can_bind_to_custom_address ||= /^\s-a\s/.match `#{OPTIONS[:spawner]} -h`
end end
end end
class MongrelSpawner < Spawner class MongrelSpawner < Spawner
def self.spawn(port) def self.spawn(port)
system("mongrel_rails start -d -p #{port} -P #{OPTIONS[:pids]}/#{OPTIONS[:process]}.#{port}.pid -e #{OPTIONS[:environment]}") cmd = "mongrel_rails start -d -p #{port} -P #{OPTIONS[:pids]}/#{OPTIONS[:process]}.#{port}.pid -e #{OPTIONS[:environment]}"
cmd << "-a #{OPTIONS[:address]}" if can_bind_to_custom_address?
system(cmd)
end
def self.can_bind_to_custom_address?
true
end end
end end
@ -99,6 +111,7 @@ def self.spawn(port)
:pids => File.expand_path(RAILS_ROOT + "/tmp/pids"), :pids => File.expand_path(RAILS_ROOT + "/tmp/pids"),
:process => "dispatch", :process => "dispatch",
:port => 8000, :port => 8000,
:address => '0.0.0.0',
:instances => 3, :instances => 3,
:repeat => nil :repeat => nil
} }
@ -110,31 +123,51 @@ def self.spawn(port)
opts.on <<-EOF opts.on <<-EOF
Description: Description:
The spawner is a wrapper for spawn-fcgi and mongrel that makes it easier to start multiple The spawner is a wrapper for spawn-fcgi and mongrel that makes it
processes running the Rails dispatcher. The spawn-fcgi command is included with the lighttpd easier to start multiple processes running the Rails dispatcher. The
web server, but can be used with both Apache and lighttpd (and any other web server supporting spawn-fcgi command is included with the lighttpd web server, but can
externally managed FCGI processes). Mongrel automatically ships with with mongrel_rails for starting be used with both Apache and lighttpd (and any other web server
dispatchers. supporting externally managed FCGI processes). Mongrel automatically
ships with with mongrel_rails for starting dispatchers.
The first choice you need to make is whether to spawn the Rails dispatchers as FCGI or Mongrel. By default,
this spawner will prefer Mongrel, so if that's installed, and no platform choice is made, Mongrel is used.
Then decide a starting port (default is 8000) and the number of FCGI process instances you'd The first choice you need to make is whether to spawn the Rails
like to run. So if you pick 9100 and 3 instances, you'll start processes on 9100, 9101, and 9102. dispatchers as FCGI or Mongrel. By default, this spawner will prefer
Mongrel, so if that's installed, and no platform choice is made,
Mongrel is used.
By setting the repeat option, you get a protection loop, which will attempt to restart any FCGI processes Then decide a starting port (default is 8000) and the number of FCGI
that might have been exited or outright crashed. process instances you'd like to run. So if you pick 9100 and 3
instances, you'll start processes on 9100, 9101, and 9102.
Examples: By setting the repeat option, you get a protection loop, which will
spawner # starts instances on 8000, 8001, and 8002 using Mongrel if available attempt to restart any FCGI processes that might have been exited or
spawner fcgi # starts instances on 8000, 8001, and 8002 using FCGI outright crashed.
spawner mongrel -i 5 # starts instances on 8000, 8001, 8002, 8003, and 8004 using Mongrel
spawner -p 9100 -i 10 # starts 10 instances counting from 9100 to 9109 using Mongrel if available You can select bind address for started processes. By default these
spawner -p 9100 -r 5 # starts 3 instances counting from 9100 to 9102 and attempts start them every 5 seconds listen on every interface. For single machine installations you would
probably want to use 127.0.0.1, hiding them form the outside world.
Examples:
spawner # starts instances on 8000, 8001, and 8002
# using Mongrel if available.
spawner fcgi # starts instances on 8000, 8001, and 8002
# using FCGI.
spawner mongrel -i 5 # starts instances on 8000, 8001, 8002,
# 8003, and 8004 using Mongrel.
spawner -p 9100 -i 10 # starts 10 instances counting from 9100 to
# 9109 using Mongrel if available.
spawner -p 9100 -r 5 # starts 3 instances counting from 9100 to
# 9102 and attempts start them every 5
# seconds.
spawner -a 127.0.0.1 # starts 3 instances binding to localhost
EOF EOF
opts.on(" Options:") opts.on(" Options:")
opts.on("-p", "--port=number", Integer, "Starting port number (default: #{OPTIONS[:port]})") { |OPTIONS[:port]| }
if spawner_class.can_bind_to_custom_address?
opts.on("-a", "--address=ip", String, "Bind to IP address (default: #{OPTIONS[:address]})") { |OPTIONS[:address]| }
end
opts.on("-p", "--port=number", Integer, "Starting port number (default: #{OPTIONS[:port]})") { |v| OPTIONS[:port] = v } opts.on("-p", "--port=number", Integer, "Starting port number (default: #{OPTIONS[:port]})") { |v| OPTIONS[:port] = v }
opts.on("-i", "--instances=number", Integer, "Number of instances (default: #{OPTIONS[:instances]})") { |v| OPTIONS[:instances] = v } opts.on("-i", "--instances=number", Integer, "Number of instances (default: #{OPTIONS[:instances]})") { |v| OPTIONS[:instances] = v }
opts.on("-r", "--repeat=seconds", Integer, "Repeat spawn attempts every n seconds (default: off)") { |v| OPTIONS[:repeat] = v } opts.on("-r", "--repeat=seconds", Integer, "Repeat spawn attempts every n seconds (default: off)") { |v| OPTIONS[:repeat] = v }