2005-06-29 07:07:20 -04:00
|
|
|
require 'fcgi'
|
|
|
|
require 'logger'
|
|
|
|
require 'dispatcher'
|
2005-09-28 04:38:26 -04:00
|
|
|
require 'rbconfig'
|
2005-06-29 07:07:20 -04:00
|
|
|
|
|
|
|
class RailsFCGIHandler
|
2005-07-02 00:52:14 -04:00
|
|
|
SIGNALS = {
|
2006-02-19 01:25:47 -05:00
|
|
|
'HUP' => :reload,
|
2006-11-11 03:08:53 -05:00
|
|
|
'INT' => :exit_now,
|
2006-02-19 01:25:47 -05:00
|
|
|
'TERM' => :exit_now,
|
|
|
|
'USR1' => :exit,
|
2007-04-28 11:57:29 -04:00
|
|
|
'USR2' => :restart
|
2005-07-02 00:52:14 -04:00
|
|
|
}
|
2006-11-11 03:08:53 -05:00
|
|
|
GLOBAL_SIGNALS = SIGNALS.keys - %w(USR1)
|
2005-07-02 00:52:14 -04:00
|
|
|
|
2005-06-29 07:07:20 -04:00
|
|
|
attr_reader :when_ready
|
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
attr_accessor :log_file_path
|
|
|
|
attr_accessor :gc_request_period
|
|
|
|
|
|
|
|
# Initialize and run the FastCGI instance, passing arguments through to new.
|
|
|
|
def self.process!(*args, &block)
|
|
|
|
new(*args, &block).process!
|
2005-06-29 07:07:20 -04:00
|
|
|
end
|
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
# Initialize the FastCGI instance with the path to a crash log
|
|
|
|
# detailing unhandled exceptions (default RAILS_ROOT/log/fastcgi.crash.log)
|
|
|
|
# and the number of requests to process between garbage collection runs
|
|
|
|
# (default nil for normal GC behavior.) Optionally, pass a block which
|
|
|
|
# takes this instance as an argument for further configuration.
|
|
|
|
def initialize(log_file_path = nil, gc_request_period = nil)
|
|
|
|
self.log_file_path = log_file_path || "#{RAILS_ROOT}/log/fastcgi.crash.log"
|
|
|
|
self.gc_request_period = gc_request_period
|
|
|
|
|
|
|
|
# Yield for additional configuration.
|
|
|
|
yield self if block_given?
|
2005-06-29 07:07:20 -04:00
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
# Safely install signal handlers.
|
|
|
|
install_signal_handlers
|
|
|
|
|
2009-02-07 01:25:07 -05:00
|
|
|
@app = Dispatcher.new
|
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
# Start error timestamp at 11 seconds ago.
|
2005-06-29 07:07:20 -04:00
|
|
|
@last_error_on = Time.now - 11
|
|
|
|
end
|
|
|
|
|
2005-07-04 15:06:50 -04:00
|
|
|
def process!(provider = FCGI)
|
2007-09-23 07:20:25 -04:00
|
|
|
mark_features!
|
|
|
|
|
|
|
|
dispatcher_log :info, 'starting'
|
|
|
|
process_each_request provider
|
|
|
|
dispatcher_log :info, 'stopping gracefully'
|
|
|
|
|
|
|
|
rescue Exception => error
|
|
|
|
case error
|
|
|
|
when SystemExit
|
|
|
|
dispatcher_log :info, 'stopping after explicit exit'
|
|
|
|
when SignalException
|
|
|
|
dispatcher_error error, 'stopping after unhandled signal'
|
|
|
|
else
|
|
|
|
# Retry if exceptions occur more than 10 seconds apart.
|
|
|
|
if Time.now - @last_error_on > 10
|
|
|
|
@last_error_on = Time.now
|
|
|
|
dispatcher_error error, 'retrying after unhandled exception'
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
dispatcher_error error, 'stopping after unhandled exception within 10 seconds of the last'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-07-02 00:52:14 -04:00
|
|
|
|
2007-09-23 07:20:25 -04:00
|
|
|
protected
|
|
|
|
def process_each_request(provider)
|
2009-02-07 01:35:15 -05:00
|
|
|
request = nil
|
2005-06-29 07:07:20 -04:00
|
|
|
|
2008-03-30 19:07:52 -04:00
|
|
|
catch :exit do
|
2009-02-07 01:25:07 -05:00
|
|
|
provider.each do |request|
|
|
|
|
process_request(request)
|
2008-03-30 19:07:52 -04:00
|
|
|
|
|
|
|
case when_ready
|
|
|
|
when :reload
|
|
|
|
reload!
|
|
|
|
when :restart
|
2009-02-07 01:35:15 -05:00
|
|
|
close_connection(request)
|
2008-03-30 19:07:52 -04:00
|
|
|
restart!
|
|
|
|
when :exit
|
2009-02-07 01:35:15 -05:00
|
|
|
close_connection(request)
|
2008-03-30 19:07:52 -04:00
|
|
|
throw :exit
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue SignalException => signal
|
|
|
|
raise unless signal.message == 'SIGUSR1'
|
2009-02-07 01:35:15 -05:00
|
|
|
close_connection(request)
|
2005-06-29 07:07:20 -04:00
|
|
|
end
|
2006-09-02 20:18:31 -04:00
|
|
|
|
2009-02-07 01:25:07 -05:00
|
|
|
def process_request(request)
|
2008-03-30 19:07:52 -04:00
|
|
|
@processing, @when_ready = true, nil
|
2007-09-23 07:20:25 -04:00
|
|
|
gc_countdown
|
|
|
|
|
|
|
|
with_signal_handler 'USR1' do
|
|
|
|
begin
|
2009-02-07 01:25:07 -05:00
|
|
|
::Rack::Handler::FastCGI.serve(request, @app)
|
2007-09-23 07:20:25 -04:00
|
|
|
rescue SignalException, SystemExit
|
|
|
|
raise
|
|
|
|
rescue Exception => error
|
|
|
|
dispatcher_error error, 'unhandled dispatch error'
|
|
|
|
end
|
|
|
|
end
|
2008-03-30 19:07:52 -04:00
|
|
|
ensure
|
|
|
|
@processing = false
|
2007-09-23 07:20:25 -04:00
|
|
|
end
|
2006-09-02 20:18:31 -04:00
|
|
|
|
2005-06-29 07:07:20 -04:00
|
|
|
def logger
|
|
|
|
@logger ||= Logger.new(@log_file_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatcher_log(level, msg)
|
|
|
|
time_str = Time.now.strftime("%d/%b/%Y:%H:%M:%S")
|
|
|
|
logger.send(level, "[#{time_str} :: #{$$}] #{msg}")
|
2006-08-30 23:16:28 -04:00
|
|
|
rescue Exception => log_error # Logger errors
|
2005-06-29 07:07:20 -04:00
|
|
|
STDERR << "Couldn't write to #{@log_file_path.inspect}: #{msg}\n"
|
|
|
|
STDERR << " #{log_error.class}: #{log_error.message}\n"
|
|
|
|
end
|
|
|
|
|
2005-09-28 04:38:26 -04:00
|
|
|
def dispatcher_error(e, msg = "")
|
2005-06-29 07:07:20 -04:00
|
|
|
error_message =
|
|
|
|
"Dispatcher failed to catch: #{e} (#{e.class})\n" +
|
|
|
|
" #{e.backtrace.join("\n ")}\n#{msg}"
|
|
|
|
dispatcher_log(:error, error_message)
|
|
|
|
end
|
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
def install_signal_handlers
|
2006-11-11 03:08:53 -05:00
|
|
|
GLOBAL_SIGNALS.each { |signal| install_signal_handler(signal) }
|
2005-07-02 00:52:14 -04:00
|
|
|
end
|
|
|
|
|
2006-11-11 03:08:53 -05:00
|
|
|
def install_signal_handler(signal, handler = nil)
|
2007-09-23 07:20:25 -04:00
|
|
|
if SIGNALS.include?(signal) && self.class.method_defined?(name = "#{SIGNALS[signal]}_handler")
|
|
|
|
handler ||= method(name).to_proc
|
2007-11-12 19:54:57 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
trap(signal, handler)
|
|
|
|
rescue ArgumentError
|
|
|
|
dispatcher_log :warn, "Ignoring unsupported signal #{signal}."
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
else
|
|
|
|
dispatcher_log :warn, "Ignoring unsupported signal #{signal}."
|
|
|
|
end
|
2005-07-02 00:52:14 -04:00
|
|
|
end
|
|
|
|
|
2006-11-11 03:08:53 -05:00
|
|
|
def with_signal_handler(signal)
|
|
|
|
install_signal_handler(signal)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
install_signal_handler(signal, 'DEFAULT')
|
|
|
|
end
|
|
|
|
|
2005-11-02 08:21:22 -05:00
|
|
|
def exit_now_handler(signal)
|
2007-09-23 07:20:25 -04:00
|
|
|
dispatcher_log :info, "asked to stop immediately"
|
2005-11-02 08:21:22 -05:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2005-10-13 16:04:13 -04:00
|
|
|
def exit_handler(signal)
|
2007-09-23 07:20:25 -04:00
|
|
|
dispatcher_log :info, "asked to stop ASAP"
|
2008-03-30 19:07:52 -04:00
|
|
|
if @processing
|
|
|
|
@when_ready = :exit
|
|
|
|
else
|
|
|
|
throw :exit
|
|
|
|
end
|
2005-06-29 07:07:20 -04:00
|
|
|
end
|
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
def reload_handler(signal)
|
|
|
|
dispatcher_log :info, "asked to reload ASAP"
|
2008-03-30 19:07:52 -04:00
|
|
|
if @processing
|
|
|
|
@when_ready = :reload
|
|
|
|
else
|
|
|
|
reload!
|
|
|
|
end
|
2005-09-28 04:38:26 -04:00
|
|
|
end
|
|
|
|
|
2005-10-13 16:04:13 -04:00
|
|
|
def restart_handler(signal)
|
2005-09-28 04:38:26 -04:00
|
|
|
dispatcher_log :info, "asked to restart ASAP"
|
2008-03-30 19:07:52 -04:00
|
|
|
if @processing
|
|
|
|
@when_ready = :restart
|
|
|
|
else
|
|
|
|
restart!
|
|
|
|
end
|
2005-06-29 07:07:20 -04:00
|
|
|
end
|
|
|
|
|
2005-09-28 04:38:26 -04:00
|
|
|
def restart!
|
|
|
|
config = ::Config::CONFIG
|
|
|
|
ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
|
|
|
|
command_line = [ruby, $0, ARGV].flatten.join(' ')
|
2006-11-11 03:08:53 -05:00
|
|
|
|
2005-09-28 04:38:26 -04:00
|
|
|
dispatcher_log :info, "restarted"
|
|
|
|
|
2007-06-23 13:04:37 -04:00
|
|
|
# close resources as they won't be closed by
|
|
|
|
# the OS when using exec
|
|
|
|
logger.close rescue nil
|
2008-08-18 23:38:58 -04:00
|
|
|
Rails.logger.close rescue nil
|
2007-06-23 13:04:37 -04:00
|
|
|
|
2005-09-28 04:38:26 -04:00
|
|
|
exec(command_line)
|
|
|
|
end
|
|
|
|
|
2005-10-13 16:04:13 -04:00
|
|
|
def reload!
|
|
|
|
run_gc! if gc_request_period
|
|
|
|
restore!
|
|
|
|
@when_ready = nil
|
|
|
|
dispatcher_log :info, "reloaded"
|
|
|
|
end
|
|
|
|
|
2007-09-23 07:20:25 -04:00
|
|
|
# Make a note of $" so we can safely reload this instance.
|
|
|
|
def mark_features!
|
2005-10-13 16:04:13 -04:00
|
|
|
@features = $".clone
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore!
|
|
|
|
$".replace @features
|
|
|
|
Dispatcher.reset_application!
|
|
|
|
ActionController::Routing::Routes.reload
|
|
|
|
end
|
2006-11-11 03:08:53 -05:00
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
def run_gc!
|
|
|
|
@gc_request_countdown = gc_request_period
|
|
|
|
GC.enable; GC.start; GC.disable
|
|
|
|
end
|
2006-11-11 03:08:53 -05:00
|
|
|
|
2005-10-13 16:04:13 -04:00
|
|
|
def gc_countdown
|
|
|
|
if gc_request_period
|
2007-09-23 07:20:25 -04:00
|
|
|
@gc_request_countdown ||= gc_request_period
|
2005-10-13 16:04:13 -04:00
|
|
|
@gc_request_countdown -= 1
|
|
|
|
run_gc! if @gc_request_countdown <= 0
|
|
|
|
end
|
|
|
|
end
|
2006-11-11 03:08:53 -05:00
|
|
|
|
2009-02-07 01:35:15 -05:00
|
|
|
def close_connection(request)
|
|
|
|
request.finish if request
|
2005-09-28 15:31:41 -04:00
|
|
|
end
|
2006-09-02 20:18:31 -04:00
|
|
|
end
|