diff options
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/commands/process/reaper.rb | 115 | ||||
-rw-r--r-- | railties/lib/commands/process/spawner.rb | 52 | ||||
-rw-r--r-- | railties/lib/commands/process/spinner.rb | 65 |
3 files changed, 232 insertions, 0 deletions
diff --git a/railties/lib/commands/process/reaper.rb b/railties/lib/commands/process/reaper.rb new file mode 100644 index 0000000000..27c05ed5ce --- /dev/null +++ b/railties/lib/commands/process/reaper.rb @@ -0,0 +1,115 @@ +require 'optparse' +require 'net/http' +require 'uri' + +if RUBY_PLATFORM =~ /mswin32/ then abort("Reaper is only for Unix") end + +class ProgramProcess + class << self + def process_keywords(action, *keywords) + processes = keywords.collect { |keyword| find_by_keyword(keyword) }.flatten + + if processes.empty? + puts "Couldn't find any process matching: #{keywords.join(" or ")}" + else + processes.each do |process| + puts "#{action.capitalize}ing #{process}" + process.send(action) + end + end + end + + def find_by_keyword(keyword) + process_lines_with_keyword(keyword).split("\n").collect { |line| + next if line.include?("inq") || line.include?("ps -ax") || line.include?("grep") + pid, *command = line.split + new(pid, command.join(" ")) + }.compact + end + + private + def process_lines_with_keyword(keyword) + `ps -ax -o 'pid command' | grep #{keyword}` + end + end + + def initialize(pid, command) + @pid, @command = pid, command + end + + def find + end + + def reload + `kill -s HUP #{@pid}` + end + + def graceful + `kill -s TERM #{@pid}` + end + + def kill + `kill -9 #{@pid}` + end + + def usr1 + `kill -s USR1 #{@pid}` + end + + def restart + `kill -s USR2 #{@pid}` + end + + def to_s + "[#{@pid}] #{@command}" + end +end + +OPTIONS = { + :action => "restart", + :dispatcher => File.expand_path(RAILS_ROOT + '/public/dispatch.fcgi') +} + +ARGV.options do |opts| + opts.banner = "Usage: reaper [options]" + + opts.separator "" + + opts.on <<-EOF + Description: + The reaper is used to reload, gracefully exit, and forcefully exit FCGI processes + running a Rails Dispatcher. This is commonly done when a new version of the application + is available, so the existing processes can be updated to use the latest code. + + The reaper actions are: + + * reload : Only reloads the application, but not the framework (like the development environment) + * graceful: Marks all of the processes for exit after the next request + * kill : Forcefully exists all processes regardless of whether they're currently serving a request + + Graceful exist is the most common and default action. But since the processes won't exist until after + their next request, it's often necessary to ensure that such a request occurs right after they've been + marked. That's what nudging is for. + + A nudge is simply a request to a URL where the dispatcher is serving. You should perform one nudge per + FCGI process you have running if they're setup in a round-robin. Be sure to do one nudge per FCGI process + across all your servers. So three servers with 10 processes each should nudge 30 times to be sure all processes + are restarted. + + Examples: + reaper -a reload + EOF + + opts.on(" Options:") + + opts.on("-a", "--action=name", "reload|graceful|kill (default: #{OPTIONS[:action]})", String) { |OPTIONS[:action]| } + opts.on("-d", "--dispatcher=path", "default: #{OPTIONS[:dispatcher]}", String) { |OPTIONS[:dispatcher]| } + + opts.separator "" + + opts.on("-h", "--help", "Show this help message.") { puts opts; exit } + + opts.parse! +end + +ProgramProcess.process_keywords(OPTIONS[:action], OPTIONS[:dispatcher])
\ No newline at end of file diff --git a/railties/lib/commands/process/spawner.rb b/railties/lib/commands/process/spawner.rb new file mode 100644 index 0000000000..465017d8a4 --- /dev/null +++ b/railties/lib/commands/process/spawner.rb @@ -0,0 +1,52 @@ +require 'optparse' + +def spawn(port) + print "Starting FCGI on port: #{port}\n " + system("#{OPTIONS[:spawner]} -f #{OPTIONS[:dispatcher]} -p #{port}") +end + +OPTIONS = { + :environment => "production", + :spawner => '/usr/bin/env spawn-fcgi', + :dispatcher => File.expand_path(RAILS_ROOT + '/public/dispatch.fcgi'), + :port => 8000, + :instances => 3 +} + +ARGV.options do |opts| + opts.banner = "Usage: spawner [options]" + + opts.separator "" + + opts.on <<-EOF + Description: + The spawner is a wrapper for spawn-fcgi that makes it easier to start multiple FCGI + processes running the Rails dispatcher. The spawn-fcgi command is included with the lighttpd + web server, but can be used with both Apache and lighttpd (and any other web server supporting + externally managed FCGI processes). + + You decide a starting port (default is 8000) and the number of FCGI 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: + spawner # starts instances on 8000, 8001, and 8002 + spawner -p 9100 -i 10 # starts 10 instances counting from 9100 to 9109 + EOF + + opts.on(" Options:") + + opts.on("-p", "--port=number", Integer, "Starting port number (default: #{OPTIONS[:port]})") { |OPTIONS[:port]| } + opts.on("-i", "--instances=number", Integer, "Number of instances (default: #{OPTIONS[:instances]})") { |OPTIONS[:instances]| } + opts.on("-e", "--environment=name", String, "test|development|production (default: #{OPTIONS[:environment]})") { |OPTIONS[:environment]| } + opts.on("-s", "--spawner=path", String, "default: #{OPTIONS[:spawner]}") { |OPTIONS[:spawner]| } + opts.on("-d", "--dispatcher=path", String, "default: #{OPTIONS[:dispatcher]}") { |dispatcher| OPTIONS[:dispatcher] = File.expand_path(dispatcher) } + + opts.separator "" + + opts.on("-h", "--help", "Show this help message.") { puts opts; exit } + + opts.parse! +end + +ENV["RAILS_ENV"] = OPTIONS[:environment] +OPTIONS[:instances].times { |i| spawn(OPTIONS[:port] + i) }
\ No newline at end of file diff --git a/railties/lib/commands/process/spinner.rb b/railties/lib/commands/process/spinner.rb new file mode 100644 index 0000000000..88c8e7cbd0 --- /dev/null +++ b/railties/lib/commands/process/spinner.rb @@ -0,0 +1,65 @@ +require 'optparse' + +def daemonize + exit if fork # Parent exits, child continues. + Process.setsid # Become session leader. + exit if fork # Zap session leader. See [1]. + Dir.chdir "/" # Release old working directory. + File.umask 0000 # Ensure sensible umask. Adjust as needed. + STDIN.reopen "/dev/null" # Free file descriptors and + STDOUT.reopen "/dev/null", "a" # point them somewhere sensible. + STDERR.reopen STDOUT # STDOUT/ERR should better go to a logfile. +end + +OPTIONS = { + :high_interval => 5.0, + :low_interval => 0.5, + :command => File.expand_path(File.dirname(__FILE__) + '/spawner'), + :daemon => false +} + +ARGV.options do |opts| + opts.banner = "Usage: spinner [options]" + + opts.separator "" + + opts.on <<-EOF + Description: + The spinner is a protection loop for the spawner, which will attempt to restart any FCGI processes + that might have been restarted or outright crashed. It's a brute-force attempt that'll just try + to run the spawner every X number of seconds, so it does pose a light load on the server. + + Examples: + spinner # attempts to run the spawner with default settings every second with output on the terminal + spinner -i 3 -d # only run the spawner every 3 seconds and detach from the terminal to become a daemon + spinner -c '/path/to/app/script/process/spawner -p 9000 -i 10' -d # using custom spawner + EOF + + opts.on(" Options:") + + opts.on("-c", "--command=path", String) { |OPTIONS[:command]| } + opts.on("-h", "--high-interval=seconds", Float) { |OPTIONS[:high_interval]| } + opts.on("-l", "--low-interval=seconds", Float) { |OPTIONS[:low_interval]| } + opts.on("-d", "--daemon") { |OPTIONS[:daemon]| } + + opts.separator "" + + opts.on("-h", "--help", "Show this help message.") { puts opts; exit } + + opts.parse! +end + +daemonize if OPTIONS[:daemon] + +trap(OPTIONS[:daemon] ? "TERM" : "INT") { exit } +trap("USR1") do + $interval = ($interval == OPTIONS[:high_interval] ? OPTIONS[:low_interval] : OPTIONS[:high_interval]) + puts "New interval: #{$interval}" +end + +$interval = OPTIONS[:high_interval] + +loop do + system(OPTIONS[:command]) + sleep($interval) +end
\ No newline at end of file |