Class: Rack::ActiveRecordReconnector

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/active_record_reconnector.rb,
lib/rack/active_record_reconnector/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ActiveRecordReconnector

param

Rack::Application app

param

Hash options



9
10
11
12
13
14
15
# File 'lib/rack/active_record_reconnector.rb', line 9

def initialize(app, options = {})
  @app         = app
  @log_level ||= detect_log_level(options)
  if options.has_key?(:logger) && !@log_level.nil?
    @logger  ||= options[:logger]
  end
end

Instance Attribute Details

#log_levelObject (readonly)

Returns the value of attribute log_level.



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

def log_level
  @log_level
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#call(env) ⇒ Object

param

Hash env

return

Rack::Response



36
37
38
39
40
41
42
43
44
# File 'lib/rack/active_record_reconnector.rb', line 36

def call(env)
  begin
    @app.call(env)
  rescue Exception => e
    logger.send(log_level, "ActiveRecord reconnect ! by #{e.inspect} from #{e.backtrace.first}") if logger
    ::ActiveRecord::Base.connection.reconnect!
    raise e
  end
end

#detect_log_level(options = {}) ⇒ Object

param

Hash options

return

Symbol or nil



22
23
24
25
26
27
28
29
30
# File 'lib/rack/active_record_reconnector.rb', line 22

def detect_log_level(options = {})
  if options.has_key? :log_level
    options[:log_level]
  else
    if options.has_key? :debug
      options[:debug] ? :debug : nil
    end
  end
end