Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5485

Show
Ignore:
Timestamp:
11/11/06 08:08:53 (2 years ago)
Author:
bitsweat
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r5471 r5485  
    11*SVN* 
     2 
     3* 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] 
    24 
    35* Alter prior change to use require_dependency instead of require_or_load. Causes ApplicationController to be reloaded again. Closes #6587. [Nicholas Seckar] 
  • trunk/railties/lib/fcgi_handler.rb

    r4913 r5485  
    77  SIGNALS = { 
    88    'HUP'     => :reload, 
     9    'INT'     => :exit_now, 
    910    'TERM'    => :exit_now, 
    1011    'USR1'    => :exit, 
     
    1213    'SIGTRAP' => :breakpoint 
    1314  } 
     15  GLOBAL_SIGNALS = SIGNALS.keys - %w(USR1) 
    1416 
    1517  attr_reader :when_ready 
     
    9395 
    9496    def install_signal_handlers 
    95       SIGNALS.each do |signal, handler_name| 
    96         install_signal_handler(signal, method("#{handler_name}_handler").to_proc) 
    97       end 
    98     end 
    99  
    100     def install_signal_handler(signal, handler) 
     97      GLOBAL_SIGNALS.each { |signal| install_signal_handler(signal) } 
     98    end 
     99 
     100    def install_signal_handler(signal, handler = nil) 
     101      handler ||= method("#{SIGNALS[signal]}_handler").to_proc 
    101102      trap(signal, handler) 
    102103    rescue ArgumentError 
     
    104105    end 
    105106 
     107    def with_signal_handler(signal) 
     108      install_signal_handler(signal) 
     109      yield 
     110    ensure 
     111      install_signal_handler(signal, 'DEFAULT') 
     112    end 
     113 
    106114    def exit_now_handler(signal) 
    107115      dispatcher_log :info, "asked to terminate immediately" 
     
    128136      @when_ready = :breakpoint 
    129137    end 
    130      
     138 
    131139    def process_each_request!(provider) 
    132       provider.each_cgi do |cgi|  
    133         process_request(cgi) 
     140      cgi = nil 
     141      provider.each_cgi do |cgi| 
     142        with_signal_handler 'USR1' do 
     143          process_request(cgi) 
     144        end 
    134145 
    135146        case when_ready 
     
    149160        gc_countdown 
    150161      end 
     162    rescue SignalException => signal 
     163      raise unless signal.message == 'SIGUSR1' 
     164      close_connection(cgi) if cgi 
    151165    end 
    152166 
     
    162176      ruby         = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT'] 
    163177      command_line = [ruby, $0, ARGV].flatten.join(' ') 
    164        
     178 
    165179      dispatcher_log :info, "restarted" 
    166180 
     
    184198      ActionController::Routing::Routes.reload 
    185199    end 
    186      
     200 
    187201    def breakpoint! 
    188202      require 'breakpoint' 
     
    198212      GC.enable; GC.start; GC.disable 
    199213    end 
    200      
     214 
    201215    def gc_countdown 
    202216      if gc_request_period 
     
    205219      end 
    206220    end 
    207      
     221 
    208222    def close_connection(cgi) 
    209223      cgi.instance_variable_get("@request").finish