Class: RailsFCGIHandler

Inherits:
Object show all
Defined in:
lib/fcgi_handler.rb

Constant Summary collapse

SIGNALS =
{
  'HUP'     => :reload,
  'INT'     => :exit_now,
  'TERM'    => :exit_now,
  'USR1'    => :exit,
  'USR2'    => :restart
}
GLOBAL_SIGNALS =
SIGNALS.keys - %w(USR1)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_file_path = nil, gc_request_period = nil) {|_self| ... } ⇒ RailsFCGIHandler

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.

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fcgi_handler.rb', line 32

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?

  # Safely install signal handlers.
  install_signal_handlers

  # Start error timestamp at 11 seconds ago.
  @last_error_on = Time.now - 11
end

Instance Attribute Details

#gc_request_periodObject

Returns the value of attribute gc_request_period.



19
20
21
# File 'lib/fcgi_handler.rb', line 19

def gc_request_period
  @gc_request_period
end

#log_file_pathObject

Returns the value of attribute log_file_path.



18
19
20
# File 'lib/fcgi_handler.rb', line 18

def log_file_path
  @log_file_path
end

#when_readyObject (readonly)

Returns the value of attribute when_ready.



16
17
18
# File 'lib/fcgi_handler.rb', line 16

def when_ready
  @when_ready
end

Class Method Details

.process!(*args, &block) ⇒ Object

Initialize and run the FastCGI instance, passing arguments through to new.



23
24
25
# File 'lib/fcgi_handler.rb', line 23

def self.process!(*args, &block)
  new(*args, &block).process!
end

Instance Method Details

#process!(provider = FCGI) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fcgi_handler.rb', line 46

def process!(provider = FCGI)
  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