diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-04-28 00:00:50 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-04-28 00:00:50 +0000 |
commit | 7a8ed3bbbf44b273622be0e324a616065f5656a8 (patch) | |
tree | 02ef3b281ef8470a43f958062582b03387e68d90 /railties/lib/commands/process/spawner.rb | |
parent | 1eade4137a96a55ed699d49c18fe177224363e7d (diff) | |
download | rails-7a8ed3bbbf44b273622be0e324a616065f5656a8.tar.gz rails-7a8ed3bbbf44b273622be0e324a616065f5656a8.tar.bz2 rails-7a8ed3bbbf44b273622be0e324a616065f5656a8.zip |
Added pid file usage to script/process/spawner and script/process/reaper along with a directive in default config/lighttpd.conf file to record the pid. They will all save their pid file in tmp/pids [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4294 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib/commands/process/spawner.rb')
-rw-r--r-- | railties/lib/commands/process/spawner.rb | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/railties/lib/commands/process/spawner.rb b/railties/lib/commands/process/spawner.rb index 84364589c8..9424dacc74 100644 --- a/railties/lib/commands/process/spawner.rb +++ b/railties/lib/commands/process/spawner.rb @@ -1,5 +1,6 @@ require 'optparse' require 'socket' +require 'fileutils' def daemonize #:nodoc: exit if fork # Parent exits, child continues. @@ -12,28 +13,52 @@ def daemonize #:nodoc: STDERR.reopen STDOUT # STDOUT/ERR should better go to a logfile. end -def spawn(port) - print "Checking if something is already running on port #{port}..." - begin - srv = TCPServer.new('0.0.0.0', port) - srv.close - srv = nil - print "NO\n " - print "Starting FCGI on port: #{port}\n " - system("#{OPTIONS[:spawner]} -f #{OPTIONS[:dispatcher]} -p #{port}") - rescue - print "YES\n" +class Spawner + def self.record_pid(name = "spawner", id = Process.pid) + FileUtils.mkdir_p(OPTIONS[:pids]) + File.open(File.expand_path(OPTIONS[:pids] + "/#{name}.pid"), "w+") { |f| f.write(id) } + end + + def self.spawn_all + OPTIONS[:instances].times do |i| + port = OPTIONS[:port] + i + print "Checking if something is already running on port #{port}..." + + begin + srv = TCPServer.new('0.0.0.0', port) + srv.close + srv = nil + + print "NO\n " + print "Starting FCGI on port: #{port}\n " + + FileUtils.mkdir_p(OPTIONS[:pids]) + spawn(port) + rescue + print "YES\n" + end + end end end - -def spawn_all - OPTIONS[:instances].times { |i| spawn(OPTIONS[:port] + i) } + +class FcgiSpawner < Spawner + def self.spawn(port) + system("#{OPTIONS[:spawner]} -f #{OPTIONS[:dispatcher]} -p #{port} -P #{OPTIONS[:pids]}/dispatch.#{port}.pid") + end end +# TODO: +# class MongrelSpawner < Spawner +# def self.spawn(port) +# end +# end + + OPTIONS = { :environment => "production", :spawner => '/usr/bin/env spawn-fcgi', :dispatcher => File.expand_path(RAILS_ROOT + '/public/dispatch.fcgi'), + :pids => File.expand_path(RAILS_ROOT + "/tmp/pids"), :port => 8000, :instances => 3, :repeat => nil @@ -84,11 +109,12 @@ ENV["RAILS_ENV"] = OPTIONS[:environment] if OPTIONS[:repeat] daemonize trap("TERM") { exit } + FcgiSpawner.record_pid loop do - spawn_all + FcgiSpawner.spawn_all sleep(OPTIONS[:repeat]) end else - spawn_all + FcgiSpawner.spawn_all end |