From 070d218c479c8cbb41e78f599110f0ea1cf0343a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 29 Sep 2005 11:49:56 +0000 Subject: Going to pull all commands into Rails git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2410 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/bin/process/reaper | 137 ------------------------------------------- railties/bin/process/spawner | 54 ----------------- railties/bin/process/spinner | 67 --------------------- 3 files changed, 258 deletions(-) delete mode 100755 railties/bin/process/reaper delete mode 100755 railties/bin/process/spawner delete mode 100755 railties/bin/process/spinner (limited to 'railties/bin') diff --git a/railties/bin/process/reaper b/railties/bin/process/reaper deleted file mode 100755 index 0ab5bbbb8a..0000000000 --- a/railties/bin/process/reaper +++ /dev/null @@ -1,137 +0,0 @@ -#!/usr/local/bin/ruby - -require 'optparse' -require 'net/http' -require 'uri' - -def nudge(url, iterations) - print "Nudging #{url}: " - - iterations.times do - Net::HTTP.get_response(URI.parse(url)) rescue nil - print "." - STDOUT.flush - end - - 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 -wwax") || line.include?("grep") - pid, *command = line.split - new(pid, command.join(" ")) - }.compact - end - - private - def process_lines_with_keyword(keyword) - `ps -wwax -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 to_s - "[#{@pid}] #{@command}" - end -end - -OPTIONS = { - :action => "graceful", - :dispatcher => File.expand_path(File.dirname(__FILE__) + '/../../public/dispatch.fcgi'), - :spinner => File.expand_path(File.dirname(__FILE__) + '/spinner'), - :toggle_spin => true, - :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. - - 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("-s", "--spinner=path", "default: #{OPTIONS[:spinner]}", String) { |OPTIONS[:spinner]| } - opts.on("-t", "--[no-]toggle-spin", "Whether to send a USR1 to the spinner before and after the reaping (default: true)") { |OPTIONS[:toggle_spin]| } - 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("usr1", OPTIONS[:spinner]) if OPTIONS[:toggle_spin] -ProgramProcess.process_keywords(OPTIONS[:action], OPTIONS[:dispatcher]) -nudge(OPTIONS[:nudge], OPTIONS[:iterations]) if OPTIONS[:nudge] -ProgramProcess.process_keywords("usr1", OPTIONS[:spinner]) if OPTIONS[:toggle_spin] diff --git a/railties/bin/process/spawner b/railties/bin/process/spawner deleted file mode 100755 index 2ac0df04f8..0000000000 --- a/railties/bin/process/spawner +++ /dev/null @@ -1,54 +0,0 @@ -#!/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 deleted file mode 100755 index f4cd33cac1..0000000000 --- a/railties/bin/process/spinner +++ /dev/null @@ -1,67 +0,0 @@ -#!/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 = { - :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 -- cgit v1.2.3