aboutsummaryrefslogtreecommitdiffstats
path: root/railties/bin
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-07-24 07:57:10 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-07-24 07:57:10 +0000
commitd75481c5808085935e94fcbf6460c7685ef26396 (patch)
tree4e80c3fc2dba459fb1aaab79836caaf2641f7b05 /railties/bin
parenteddd7c453b9f34ec95b64d1dee9b8fcf560a4b29 (diff)
downloadrails-d75481c5808085935e94fcbf6460c7685ef26396.tar.gz
rails-d75481c5808085935e94fcbf6460c7685ef26396.tar.bz2
rails-d75481c5808085935e94fcbf6460c7685ef26396.zip
Added convenience controls for FCGI processes (especially when managed remotely): spinner, spawner, and reaper. They reside in script/process. More details can be had by calling them with -h/--help
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1909 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/bin')
-rwxr-xr-xrailties/bin/process/reaper123
-rwxr-xr-xrailties/bin/process/spawner54
-rwxr-xr-xrailties/bin/process/spinner60
3 files changed, 237 insertions, 0 deletions
diff --git a/railties/bin/process/reaper b/railties/bin/process/reaper
new file mode 100755
index 0000000000..a636d329c8
--- /dev/null
+++ b/railties/bin/process/reaper
@@ -0,0 +1,123 @@
+#!/usr/local/bin/ruby
+
+require 'optparse'
+require 'net/http'
+require 'uri'
+
+def nudge(url, iterations)
+ print "Nudging #{url}: "
+ iterations.times { Net::HTTP.get_response(URI.parse(url)); print "."; STDOUT.flush }
+ puts
+end
+
+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 to_s
+ "[#{@pid}] #{@command}"
+ end
+end
+
+OPTIONS = {
+ :action => "graceful",
+ :dispatcher => File.expand_path(File.dirname(__FILE__) + '/../../public/dispatch.fcgi'),
+ :iterations => 10,
+ :nudge => false
+}
+
+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.
+
+ NOTE: You're responsible for restarting the processes after they exit. This can be automated by using
+ the spinner.
+ Examples:
+ reaper -a reload
+ reaper -n http://www.example.com -i 10 # gracefully exit, nudge 10 times
+ 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.on("-n", "--nudge=url", "Should point to URL that's handled by the FCGI process", String) { |OPTIONS[:nudge]| }
+ opts.on("-i", "--iterations=number", "One nudge per FCGI process running (default: #{OPTIONS[:iterations]})", Integer) { |OPTIONS[:iterations]| }
+
+ opts.separator ""
+
+ opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
+
+ opts.parse!
+end
+
+ProgramProcess.process_keywords(OPTIONS[:action], OPTIONS[:dispatcher])
+nudge(OPTIONS[:nudge], OPTIONS[:iterations]) if OPTIONS[:nudge] \ No newline at end of file
diff --git a/railties/bin/process/spawner b/railties/bin/process/spawner
new file mode 100755
index 0000000000..2ac0df04f8
--- /dev/null
+++ b/railties/bin/process/spawner
@@ -0,0 +1,54 @@
+#!/usr/local/bin/ruby
+
+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(File.dirname(__FILE__) + '/../../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/bin/process/spinner b/railties/bin/process/spinner
new file mode 100755
index 0000000000..ff7a576163
--- /dev/null
+++ b/railties/bin/process/spinner
@@ -0,0 +1,60 @@
+#!/usr/local/bin/ruby
+
+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 = {
+ :interval => 1.0,
+ :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 load on the server (~1% on our test
+ 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("-i", "--interval=seconds", Float) { |OPTIONS[: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]
+
+loop do
+ system(OPTIONS[:command])
+ sleep(OPTIONS[:interval])
+end
+
+trap(OPTIONS[:daemon] ? "TERM" : "INT") { exit } \ No newline at end of file