Skip to content

Commit

Permalink
Add grep-based fallback to reaper, so it can work in pidless setups (…
Browse files Browse the repository at this point in the history
…again)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5488 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jamis committed Nov 11, 2006
1 parent 4c7dcb5 commit da3eae4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Add grep-based fallback to reaper, to work in pidless setups [Jamis Buck]

* Only wrap request processing with our USR1 signal handler so FastCGI can trap it and raise an exception while waiting for connections. Idle processes exit immediately rather than waiting for another request; active processes gracefully exit when the request is finished. [Jeremy Kemper] * Only wrap request processing with our USR1 signal handler so FastCGI can trap it and raise an exception while waiting for connections. Idle processes exit immediately rather than waiting for another request; active processes gracefully exit when the request is finished. [Jeremy Kemper]


* Alter prior change to use require_dependency instead of require_or_load. Causes ApplicationController to be reloaded again. Closes #6587. [Nicholas Seckar] * Alter prior change to use require_dependency instead of require_or_load. Causes ApplicationController to be reloaded again. Closes #6587. [Nicholas Seckar]
Expand Down
34 changes: 24 additions & 10 deletions railties/lib/commands/process/reaper.rb
Expand Up @@ -11,8 +11,8 @@ class << self
# set of processes: # set of processes:
# #
# Killer.process(:reload, "/tmp/pids", "dispatcher.*.pid") # Killer.process(:reload, "/tmp/pids", "dispatcher.*.pid")
def process(action, pid_path, pattern) def process(action, pid_path, pattern, keyword)
new(pid_path, pattern).process(action) new(pid_path, pattern, keyword).process(action)
end end


# Forces the (rails) application to reload by sending a +HUP+ signal to the # Forces the (rails) application to reload by sending a +HUP+ signal to the
Expand Down Expand Up @@ -45,15 +45,16 @@ def usr1(pid)
end end
end end


def initialize(pid_path, pattern) def initialize(pid_path, pattern, keyword=nil)
@pid_path, @pattern = pid_path, pattern @pid_path, @pattern, @keyword = pid_path, pattern, keyword
end end


def process(action) def process(action)
pids = find_processes pids = find_processes


if pids.empty? if pids.empty?
puts "Couldn't find any pid file in '#{@pid_path}' matching '#{@pattern}'" warn "Couldn't find any pid file in '#{@pid_path}' matching '#{@pattern}'"
warn "(also looked for processes matching #{@keyword.inspect})" if @keyword
else else
pids.each do |pid| pids.each do |pid|
puts "#{action.capitalize}ing #{pid}" puts "#{action.capitalize}ing #{pid}"
Expand All @@ -70,7 +71,18 @@ def terminating?(action)
end end


def find_processes def find_processes
pid_files.collect { |pid_file| File.read(pid_file).to_i } files = pid_files
if files.empty?
find_processes_via_grep
else
files.collect { |pid_file| File.read(pid_file).to_i }
end
end

def find_processes_via_grep
lines = `ps axww -o 'pid command' | grep #{@keyword}`.split(/\n/).
reject { |line| line =~ /inq|ps axww|grep|spawn-fcgi|spawner|reaper/ }
lines.map { |line| line[/^\s*(\d+)/, 1].to_i }
end end


def delete_pid_files def delete_pid_files
Expand All @@ -84,9 +96,10 @@ def pid_files




OPTIONS = { OPTIONS = {
:action => "restart", :action => "restart",
:pid_path => File.expand_path(RAILS_ROOT + '/tmp/pids'), :pid_path => File.expand_path(RAILS_ROOT + '/tmp/pids'),
:pattern => "dispatch.[0-9]*.pid" :pattern => "dispatch.[0-9]*.pid",
:dispatcher => File.expand_path("#{RAILS_ROOT}/public/dispatch.fcgi")
} }


ARGV.options do |opts| ARGV.options do |opts|
Expand Down Expand Up @@ -124,6 +137,7 @@ def pid_files
opts.on("-a", "--action=name", "reload|graceful|kill (default: #{OPTIONS[:action]})", String) { |v| OPTIONS[:action] = v } opts.on("-a", "--action=name", "reload|graceful|kill (default: #{OPTIONS[:action]})", String) { |v| OPTIONS[:action] = v }
opts.on("-p", "--pidpath=path", "default: #{OPTIONS[:pid_path]}", String) { |v| OPTIONS[:pid_path] = v } opts.on("-p", "--pidpath=path", "default: #{OPTIONS[:pid_path]}", String) { |v| OPTIONS[:pid_path] = v }
opts.on("-r", "--pattern=pattern", "default: #{OPTIONS[:pattern]}", String) { |v| OPTIONS[:pattern] = v } opts.on("-r", "--pattern=pattern", "default: #{OPTIONS[:pattern]}", String) { |v| OPTIONS[:pattern] = v }
opts.on("-d", "--dispatcher=path", "DEPRECATED. default: #{OPTIONS[:dispatcher]}", String) { |v| OPTIONS[:dispatcher] = v }


opts.separator "" opts.separator ""


Expand All @@ -132,4 +146,4 @@ def pid_files
opts.parse! opts.parse!
end end


Killer.process(OPTIONS[:action], OPTIONS[:pid_path], OPTIONS[:pattern]) Killer.process(OPTIONS[:action], OPTIONS[:pid_path], OPTIONS[:pattern], OPTIONS[:dispatcher])

0 comments on commit da3eae4

Please sign in to comment.